Jump to content
  • Advertisement
Sign in to follow this  
Winegums

Overloading the += operator

This topic is 3938 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!