## Recommended Posts

Winegums    286
Hi, so I've been trying to overload the += operator in my application. At first my code looked like this:
Vector2 operator += (const Vector2 other)
{
Vector2 result = *this;
result.x = result.x + other.x;
result.y = result.y + other.y;

return result;
}


which didn't work (and I don't see why it doesn't work)...then i changed to this:
Vector2 operator += (const Vector2 other)
{
Vector2 result = *this;
result.x = result.x + other.x;
result.y = result.y + other.y;

*this = result;   //Just change this internally?

return result;
}


and it works, but not understanding why something works is about as bad as it not working at all.

##### Share on other sites
Mike.Popoloski    3258
Well, your first problem is that you are dereferencing the this pointer and storing it in a local variable, so when you make changes to the result variable, they aren't affecting the data in the original class. I suggest you read up on pointers again.

Secondly, I believe that in C++ you should use reference parameters to avoid unnecessary copying of data. For example:

Vector2 &operator += (const Vector2 &other) {    this->x += other.x;    this->y += other.y;    return *this;}

##### Share on other sites
darookie    1441
You failed to reproduce the semantics of the += operator in your code:
// using a const-reference avoids passing by valueVector2& operator += (const Vector2 & other) {    // modify the value of "this" directly    this->x += other.x;    this->y += other.y;    // return reference to self    return *this;}

Edit: too slow [smile]

##### Share on other sites
Oluseyi    2103
They're both wrong. Sorta.

Vector2 & operator += (const Vector2 & rhs){  x += rhs.x;  y += rhs.y;  return *this;}

It is important to return a reference to the object being modified, as that allows for operator chaining as expected. You're creating a temporary object which you modify in your first example, but that modification is not reflected in the object you are calling the operator on (this). In your second example, you do modify this (by assigning the value of result) to it, but result is a superfluous temporary.

##### Share on other sites
darookie    1441
Quote:
 Original post by OluseyiThey're both wrong. Sorta.

Very brief. Yes. Wrong - how?

##### Share on other sites
MaulingMonkey    1728
Quote:
 Original post by Winegumswhich didn't work (and I don't see why it doesn't work)

It "works" in that it does something, just not what you expect. Let's break it down section by section, red indicating where it's most likely your understanding fell short.

// Given: Vector2 A, B;//        A += B;Vector2 operator += (const Vector2 other){	//   this == &A -- they are the same variable	//  other ==  B -- they have the same value	Vector2 result = *this;	// Okay, here we've created a copy of A named result.	// &result != &A -- they are seperate variables	result.x = result.x + other.x;	result.y = result.y + other.y;	// We modify the copy we made.	return result; 	// Here we return a copy of our copy we made.	// In the expression "A += B;", this return value is discarded.	// Here is an example that would use the return value: "C = (A += B);"	// Neither A nor B were modified by the end of this expression, only the copy of A, "result".	// "result" is destroyed when the function ends -- it no longer exists}

##### Share on other sites
Oluseyi    2103
Quote:
Original post by darookie
Quote:
 Original post by OluseyiThey're both wrong. Sorta.

Very brief. Yes. Wrong - how?

Quote:
 Original post by Oluseyi - same post as quotedYou're creating a temporary object which you modify in your first example, but that modification is not reflected in the object you are calling the operator on (this)....In your second example, you do modify this (by assigning the value of result) to it, but result is a superfluous temporary.

##### Share on other sites
Mike.Popoloski    3258
I believe that darookie thought that you were saying that his and my examples were wrong, which is how I read your post at first as well.