Jump to content
  • Advertisement
Sign in to follow this  
rakoon2

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.

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

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


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


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


Link to post
Share on other sites
So I should use:


// operators
inline 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 this post


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


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


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


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


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

Yup, there were extensive talks about this last year with respect to boost's operator library. Conclusion.

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!