Sign in to follow this  
Winegums

Overloading the += operator

Recommended Posts

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 this post


Link to post
Share on other sites
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 this post


Link to post
Share on other sites
You failed to reproduce the semantics of the += operator in your code:

// using a const-reference avoids passing by value
Vector2& 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 this post


Link to post
Share on other sites
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 this post


Link to post
Share on other sites
Quote:
Original post by Winegums
which 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 this post


Link to post
Share on other sites
Quote:
Original post by darookie
Quote:
Original post by Oluseyi
They're both wrong. Sorta.

Very brief. Yes. Wrong - how?

Quote:
Original post by Oluseyi - same post as quoted
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.

Reading is Fundamental!™ [smile]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this