Original Post
Object& Object::operator=( const Object& ) { return *this; } the operator = assign the self to self?
Point2D Point2D::operator = (const Point2D& p){ this->x = p.x; this->y = p.y; return *this;}Object& Object::operator=( const Object& RHS ){ // Copy all of the members in the Object class over to this instance return *this;}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; }};
class MyString{private: char* mv_pStringBuffer; size_t mv_nSizeOfString; // Various methods ...public: MyString& operator = ( const MyString& rSource );};// in the cpp fileMyString& 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;}This topic has been locked by a moderator. New replies are not allowed.
With your permission, GameDev.net uses analytics cookies to understand how people use the platform. You can accept analytics or continue with necessary cookies only. Learn more