# operator Overloading

Printable View

• 06-29-2007
noobcpp
operator Overloading
I am facing Problem while overloading the += Operator
I've posted My Code here http://phpfi.com/245888 So that you can read it easily. The problems are Commented Out there with
Code:

`//Here is the Overloading Problem`
Here I am re describing it.
This is the Prototype
Code:

`                Point operator +=(int);//Overloading += Operator`
This is the operator Function
Code:

```Point Point::operator+=(int inc){         this->x += inc;         this->y += inc;         return *this; }```
And this is how I am using this
Code:

`pt2+=5;//here is the problem`
The Problem is the Output
Code:

```Describing Class :              pt1            x = 15  y = 10 Describing Class : pt2 before Copying          x = 4  y = 8 Describing Class : pt2 after Copying            x = 15  y = 10 Describing Class : pt2 after Increment          x = 19  y = 0```
It shouldn't Be 19 and 0
Whatever I Do with it Its always 19 and 0 Why ??
How can I solve it ??
I an using g++ on Linux
• 06-29-2007
laserlight
Why not post the smallest and simplest compilable code that demonstrates the error? So far it looks okay, except that operator+= typically returns a reference. Perhaps the mistake lies in the function that prints the member variables, for example.
• 06-29-2007
Daved
>> pt2+=5;

Your problem is that pt2 is a pointer, so you are doing a pointer increment, you are not calling your operator +=.

Don't make p1 and p2 pointers, there's no reason to in this example (and you have memory leaks as well). Just make them local objects:
Code:

```Point pt1(15,10); Point pt2(4,8);```
If you need them to be pointers for some reason, then to call the operator += you would do this:
Code:

`(*p2) += 5;`
• 06-29-2007
pianorain
You're not calling the increment operator that you've overloaded; you're just incrementing the pointer ahead 5 times. By the same token, you're not calling the assignment operator that you've overloaded; you're just doing pointer assignment. Try this:
Code:

```int main(){         Point* pt1 = new Point(15, 10);         pt1->desc("                pt1");         Point *pt2 = new Point(4, 8);         pt2->desc("pt2 before Copying");         (*pt2)=(*pt1);//Works fine         pt2->desc("pt2 after Copying");         (*pt2)+=5;//here is the problem         pt2->desc("pt2 after Increment");         return 0; }```
You'll see that your increment operator works fine, while your assignment operator could use some work.
• 06-29-2007
ZuK
Code:

`        pt2+=5;//here is the problem`
exactly. since pt2 is a pointer to Point this doesn't call the += operator.
It just advances the pointer by 5 * sizeof( Point ) and then you print an uninitialized Point object.
You're lucky that this doesn't crash.
Works ok if you use objects like this
Code:

```int main(){         Point p(15,10);         p+=2;         p.desc("");         return 0; }```
BTW: this is a rather unusual operator =
Code:

`Point* Point::operator=(const Point& pt)`
Kurt
• 06-29-2007
laserlight
Oops, my bad. I either did not see the link to the pastebin, or it was edited in quickly :p
• 06-29-2007
noobcpp
Thanks To all.
I didnt Understand this.
Quote:

Originally Posted by Zuk
It just advances the pointer by 5 * sizeof( Point ) and then you print an uninitialized Point object.
You're lucky that this doesn't crash.

What happens phisically
does it allocates more memmory for that Pointer ??
or It moves the Point to the Previous Address to a new Address ??
Why the x and y members of the pt2 Object Gets Changed ??
Quote:

Describing Class : pt2 after Increment x = 19 y = 0
• 06-29-2007
ZuK
No memory is allocated. You are printing an object that is physically 5 objects away ftrom the one that you allocated. It holds random data.
Kurt
• 06-29-2007
noobcpp
Another More Question
Whats teh Defference between
*pt
and (*pt)
??
• 06-29-2007
laserlight
No difference. There is only a difference in conjunction with other operators, e.g. (*pt).member is equivalent to pt->member but not *pt.member
• 06-29-2007
noobcpp
K thank you all very much