Archived

This topic is now archived and is closed to further replies.

Floating

= operator overloading

Recommended Posts

Hi, Can you please tell me what is wrong with the following class? Class CMyObject header:
void operator= (const CMyObject& obj);
Class CMyObject source:
void CMyObject::operator= (const CMyObject& obj)
{
   ... copy all values from obj to this
}
and somewhere else I have:
CMyObject objectA;
objectA.initialise(values...);
CMyObject objectB=objectA;
I always get my computer to crash. What is wrong? Is it possible that the above call doesn''t call the overloaded = operator? And what happens if = is not overloaded? What is the default behaviour? What would my objectB contain? Thanks a lot

Share this post


Link to post
Share on other sites
Yes it is. In this case:
CMyObject objectB=objectA; 
you''re calling the copy constructor, because you''re initializing objectB with objectA.

To call the operator=, something like this is required:


CMyObject objectA;
CMyObject objectB;

objectB = objectA;

Share this post


Link to post
Share on other sites
quote:
Original post by daerid
...
you''re calling the copy constructor, because you''re initializing objectB with objectA.
...



Will the copy constructor make objectB exactly identical to objectA?
Then if I point to an objectC in objectA, objectB will point to the same objectC (not a copy of it) right?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
you need to provide a copy constructor like this.

in CMyObject add

CMyObject( CMyObject &rhs)
{
*this = rhs;

}

Share this post


Link to post
Share on other sites
quote:
Original post by Floating
Will the copy constructor make objectB exactly identical to objectA?
Then if I point to an objectC in objectA, objectB will point to the same objectC (not a copy of it) right?

In the case of pointers inside your class, usually you will need to perform a deep copy, which actually makes a copy of what any pointers are pointing to for the object being assigned to. If you just copy the pointers, then you run into trouble at destruction time when both objects try to free the same pointer.

And "exactly identical" is a bit misleading. As I mentioned above, you will want to make copies of data that''s being pointed to. Obviously then the pointer values themselves will be different for each class, and nothing will be truly identical. However, rather than thinking of things in terms of identical, think in terms of operators. After the copy constructor is done, B == A should be true. Then you can implement the equality operator any way you wish and define "identical" in your own terms.

Share this post


Link to post
Share on other sites
Your class's assignment operator should check that the user is not assigning an object to the same object. Have a think about why, or have a search on Google for an example. You would generally do something like so:

class CMyObject
{
public:
...
CMyObject& operator=( const CMyObject& rhs );
...
private:
...
};

CMyObject& CMyObject::operator=( const CMyObject& rhs )
{
if ( this == &rhs ) // or if ( this != &rhs ) {do stuff}, return *this; ... or what ever your style is. You get the idea.

return *this;

// do stuff


return *this;
}


[ Google || Start Here || ACCU || STL || Boost || MSDN || GotW || MSVC++ Library Fixes || BarrysWorld || E-Mail Me ]

[edited by - Lektrix on July 4, 2003 8:38:16 AM]

Share this post


Link to post
Share on other sites