Jump to content
  • Advertisement
Sign in to follow this  
avocados

Can't overload operator=

This topic is 2614 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

Wiki says you can overload this in C++ but Visual Studio 2010 says it is not a member function. Just curious, what's up?
void operator=( v3f &a, v3i b ) { ; } //error
void operator-=( v3f &a, v3i b ) { ; } //no error

Share this post


Link to post
Share on other sites
Advertisement
For some reason operator= must be inside the class definition. Make it a member of v3f and it will work fine.

Share this post


Link to post
Share on other sites

Wiki says you can overload this in C++ but Visual Studio 2010 says it is not a member function. Just curious, what's up?
void operator=( v3f &a, v3i b ) { ; } //error
void operator-=( v3f &a, v3i b ) { ; } //no error







For some reason operator= must be inside the class definition. Make it a member of v3f and it will work fine.

That is not weird at all as this is an assignment operator and if you overload an assignment operator you'd better implement a copy constructor and destructor for that class as well(more info). The usual prototype for this is:

T& operator=(T rhs)
{
//Do your stuff
return *this;
}

T can stand for any user defined type here and should be the class name this method is part of.
You return a reference to this so that you can chain assignments which C++ allows you to do.
It makes no sense to declare this particular operator outside of a class as you wouldn't be able to assign the private variables form rhs to your current object.

If you would like to assign another type to this particular variable type you need to add a constructor to this class that takes that type as a parameter and can construct an instance of this type. Read up on construction of objects in C++ to understand why this happens this way, you should also know what happens when a copy constructor or assignment operator is called, as sloppy implementations can cause a overhead or even worse problems(shallow copies).

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!