Operator overloading and memory management

Hello,

I want to add overloaded operators to my Point class that allow me to do operations like:

1
2
3
4
Point* p1 = new Point(5, 10);
Point* p2 = new Point(1, 5);

Point* p3 = 2 * p1 + p2; // Should be: [11, 25] 


Now, how would I manage the memory allocation of these variables?

2 * p1
Should create a new point, [10, 20]. Should I allocate its memory (by initializing a new Point in the operator function)? Because if I do, when will it ever be released from memory?
Or is there another obvious way I'm missing? The whole reason I want these overloaded operators is because I want it to be easy to work with these Points.

Thanks!
Or is there another obvious way I'm missing?

Yes, stop using new. Memory management is usually not the programmer's job.
This is how it's done correctly:

1
2
3
4
Point p1(5,10);
Point p2(1,5);

Point p3=2*p1+p2;



There, no memory allocations or deallocations to worry about.
You don't have to worry about allocation. Just create a Point in your operator body, put the result inside and return it. The compiler will allocate a copy of that Point that can be usedd in the second operator, whose return value will be used in the assignment operator. No need to worry about dynamic allocation and liberation
So, if I have

1
2
3
4
5
6
Point* operator*(float f) {
		Point p;
		p.x = x * f;
		p.y = y * f;
		return &p;
	}


The point created there will be valid outside this function, but also be deallocated automatically later? Suppose I use it in my main function:

1
2
3
4
5
6
int main(int argc, char **argv) {
Point p1(5, 10);

Point p3 = 2 * p1;

}


Its scope would be the whole main function (and it would be deleted after that)?

Just curious if I have that right!

Thanks
Almost there. Now that you stopped using new, you also need to stop using pointers inappropriately. In your operator*, you're returning a pointer to a local variable which is destroyed as soon as the function returns.

1
2
3
4
Point operator*(double a,const Point& p)
{
    return Point(p.x*a,p.y*a);
}


And yes, you don't have to worry about memory as long as you don't use new.
Last edited on
Oh, yeah.. Had removed that myself but forgot to remove it here. Thanks for the help. I'll try not to use pointer too much. I love them.

Now my operator function looks like this:

 
Point operator*(float f) {..


If I change it to

 
Point operator*(float f, const Point& p) {..


it gives an error saying "binary 'operator *' has too many parameters. Why is that?
Probably because you're trying to declare it inside the Point class. In that case the left operand is automatically the Point instance and there is only one parameter.
Ah, figures. And what about if I want both p1 * 5.0 and 5.0 * p1 be possible? I need to declare it outside the class in that case then?

Cheers, thanks for the help!
I need to declare it outside the class in that case then?

Yes, exactly (you can declare a non-member friend function inside the class though).
Last edited on
Topic archived. No new replies allowed.