Jump to content
  • Advertisement
Sign in to follow this  
luasitdown

operator =

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

Advertisement
You can overload the operator = with what you want.
In general it's used to copy the object passed as variable into this object.


Point2D Point2D::operator = (const Point2D& p){
this->x = p.x;
this->y = p.y;
return *this;
}


Share this post


Link to post
Share on other sites
Sorta, you will need to do this though:

Object& Object::operator=( const Object& RHS )
{
// Copy all of the members in the Object class over to this instance
return *this;
}


So for example:

class Object
{
int foo;
char bar;

Object& Object::operator=( const Object& RHS )
{
foo = RHS.foo;
bar = RHS.bar;
// Copy all of the members in the Object class over to this instance
return *this;
}
};

Share this post


Link to post
Share on other sites
It assigns the passed-in argument to itself (you must write the code to do that; and doing so requires that you provide a name for the parameter). It also returns itself, so that you can chain the operator (i.e. so that "a=b=c=d" works as expected, making all equal to d).

Share this post


Link to post
Share on other sites
In some cases, great care must be taken to avoid screwups if the source is the same as the instance the operator function is invoked on.

Example:


class MyString
{
private:
char* mv_pStringBuffer;
size_t mv_nSizeOfString;

// Various methods ...
public:
MyString& operator = ( const MyString& rSource );

};

// in the cpp file
MyString& MyString::operator = ( const MyString& rSource )
{
// If we already have a buffer ...
if ( mv_pStringBuffer != 0x00 )
{
deallocateBuffer(); // HAH! GOTCHA! What if this == &rSource?? We have
// deallocated the source object's buffer as well!
}

// Allocate buffer large enough to hold content of source and do the
// coyping.
// ...

return *this;
}

// This version is better.
MyString& MyString::operator = ( const MyString& rSource )
{
// Check against self assignment.
if ( this == &rSource )
{
return *this;
}

// If we already have a buffer ...
if ( mv_pStringBuffer != 0x00 )
{
deallocateBuffer();
}

// Allocate buffer large enough to hold content of source and do the
// coyping.
// ...

return *this;
}



Of course, assigning a string to itself doesn't really make much sense, but what's to stop you from doing it anyway?

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!