Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I was looking at the d3dx math .h and noticed that a difference between the "+" and "+=" operators:

D3DXVECTOR3& operator += ( CONST D3DXVECTOR3& );
D3DXVECTOR3 operator + ( CONST D3DXVECTOR3& ) const;

Why is it returning a reference in the case of a += operation and a copy in the case of + operation ?

I'm guessing in the first case it's modifying the vector on which you call the += operation, something like this:

this->_x += x;
...
return *this;

and in the case of a + operation it would be doing something like this:

return D3DXVECTOR3(this->_x + x, this->_y + y, this->_z + z);

That being said, if that's what happens in the D3DXVECTOR3 implementation, why not return a reference in the case of + operation ?

share|improve this question
    
If the implication you're making is that returning a reference would be faster, that may not actually be the case. cpp-next.com/archive/2009/08/want-speed-pass-by-value –  Tetrad Oct 29 '10 at 19:44
add comment

3 Answers

up vote 3 down vote accepted

You return a reference to the original vector in a += because you are modifying the actual vector itself and the vector will not fall out of scope.

While in a + you do not want any modification of the original vector. Instead a + operator computes a new value and uses copy constructors to return the value.

If you would try something like

D3DXVECTOR3 newVal = D3DXVECTOR3(this->_x + x, this->_y + y, this->_z + z);
return &newVal;

You would return a reference to a vector which fell out of scope and therefore will be cleaned up very soon.

share|improve this answer
    
I guess I thought that as long as you had a ref to newVal it would not be able to be cleaned up since it would be referenced somewhere else. –  dotminic Oct 29 '10 at 19:16
    
In C++ classes/structs which do not allocate space for themselves with the "new" command will be cleaned up when the next "}" is seen. The problem is that even though it's cleaned up the values still exist in memory until something writes over that area. Which can lead to very hard to find bugs. –  David Young Oct 29 '10 at 19:20
    
Right so it's the same as in C. I should have figured that out. I'm moving up from C to C++ and was pretty unsure about that. Thanks for the info. –  dotminic Oct 29 '10 at 19:35
    
Ah then just to clarify, plain old C++ is like that, but not C++ .NET.... C++ .NET is garbage collected while plain old C++ requires new/delete –  David Young Oct 29 '10 at 19:48
add comment

Why is a reference returned for += ?

A reference is returned because the object is (likely) being modified directly, and as such it wouldn't make sense to create a copy for the sole purpose of returning one.

Note that the reference can still be converted to a copy by other operations (such as =):

D3DXVECTOR3 a (1,2,3);
D3DXVECTOR3 b (1,2,3);
a+=b;                 //no copy made
D3DXVECTOR3 c = a+=b; //one copy made

So this approach (returning by reference) allows for the least amount of work to be done in each situation.

Why is a reference NOT returned for + ?

A reference is not returned for + because a copy is made as part of the operation, so it's returned. Consider what would happen if you added two D3DXVECTOR3's together, like this: (a + b). Which of the two objects should the result be referencing? Well obviously the answer is neither - the result should be a copy. Hence a copy is returned.

share|improve this answer
add comment

In the operator += case, the method is probably modifying the calling object in place. On the other hand, operator + is creating a new D3DXVECTOR3 that is the sum of the calling object and the argument.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.