operator+-*.. question

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

Recommended Posts

Hi all! What should I use: This:
inline yae::Vector2D operator+( const yae::Vector2D& v )
{
x += v.x;
y += v.y;
return *this;
}


Or this one:
inline yae::Vector2D operator+( const yae::Vector2D& v ) const
{
return yae::Vector2D(x+v.x, y+v.y);
}


? ( and all those others * / - + ^ ... ) Thank you! :)

Share on other sites
I suggest the second one. A programmer does not usually expect a binary operator to actually alter either one of the arguments; only to return the result. If the programmer wants one of the arguments to equal the result, then he will use the = operator to do that. I realize that speed is a bit of an issue, as this is somewhat inefficient this way. But then the +=, -=, etcetera can do the job of the first sample you have.

Share on other sites
Those are different operators. The first one has the same effect as +=, the second has the same effect as +. So go with the second.

Share on other sites
So I should use:

// operatorsinline  Vector2D operator+( const Vector2D& v ) const{   return Vector2D( x+v.x, y+v.y );}inline  Vector2D operator-( const Vector2D& v ) const{   return Vector2D( x-v.x, y-v.y );}inline  Vector2D operator+=( const Vector2D& v ){   x += v.x;   y += v.y;   return *this;}inline Vector2D operator-=( const Vector2D& v ){   x -= v.x;   y -= v.y;   return *this;}

:)

Share on other sites
Yup. Here are the operators I support for my crappy vector class:
struct SpectrumVector{	SpectrumVector() {}	SpectrumVector(float _x, float _y) : x(_x), y(_y) {}	SpectrumVector(const SpectrumVector& rhs) {x=rhs.x; y=rhs.y;}	SpectrumVector& operator += (const SpectrumVector& rhs)		{x += rhs.x; y += rhs.y; return *this;}	SpectrumVector& operator -= (const SpectrumVector& rhs)		{x -= rhs.x; y -= rhs.y; return *this;}	SpectrumVector operator + (const SpectrumVector& rhs) const		{return SpectrumVector(x+rhs.x,y+rhs.y);}	SpectrumVector operator - (const SpectrumVector& rhs) const		{return SpectrumVector(x-rhs.x,y-rhs.y);}	SpectrumVector operator * (float f) const		{return SpectrumVector(x*f,y*f);}	SpectrumVector operator / (float f) const		{return SpectrumVector(x/f,y/f);}	SpectrumVector operator * (const SpectrumVector& rhs) const		{return SpectrumVector(x*rhs.x,y*rhs.y);}	SpectrumVector operator / (const SpectrumVector& rhs) const		{return SpectrumVector(x/rhs.x,y/rhs.y);}	float x,y;};

Share on other sites
one good way to overload the general arithmetic operators is to compose them out of the arithmetic assigmenet operators e.g.

template < typename T >struct vector2 {  /* ....stuff... */  template < typename U >  vector2<T>& operator+=(const vector2<U>& bar) {     x += bar.x;     y += bar.y;     return *this;  }};template < typename T >inline vector2<T> operator+(const vector2<T>& a, const vector2<T>& b) {   return (vector2<T>(a) += b);}

instead of repeating basically the same code over and over.

[Edited by - snk_kid on October 7, 2004 12:30:38 PM]

Share on other sites
Rokoon2, note how when other people return *this, their return type is TYPE &, not TYPE. This allows you to return a reference to the exact same object, instead of a copy of the object.

Share on other sites
Actually, the version which allows for the most standard optimizations is neither of the ones you showed (your first one technically isn't even addition). Your best bet is to actually do this:

inline yae::Vector2D operator+( const yae::Vector2D& v ) const{  Vector2D const addition_result( x + v.x                                , y + v.y                                );  return addition_result;}

This version can be optimized more than your proposed addition operator because of the named return value optimization defined in the standard which states that if all return paths of a function return the same, named local object, and the cv-unqualified type of that object is the same as the function's return type, then the local can actually be constructed in place where the result of the function call is instead of locally and then making a copy. With other versions, you would have a new object constructed, then copy constructed to the returned value outside of the function since the standard isn't as leniant about unnamed temporaries. This version eliminates the intermediate temporary with a compiler which uses NRVO.

Also, I recommend you change your operator to be a nonmember function to allow for left-hand conversions prior to addition.

Share on other sites
Polymorphic OOP, are you sure about that? According to Meyer returning constructor arguments instead of local objects allows for compilers to perform return value optimization, not the other way around.

- Houdini

Share on other sites
Quote:
 Original post by HoudiniPolymorphic OOP, are you sure about that? According to Meyer returning constructor arguments instead of local objects allows for compilers to perform return value optimization, not the other way around.- Houdini

• Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 11
• 10
• 9
• 15
• 22