= operator overloading

Started by
7 comments, last by Floating 20 years, 9 months ago
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
Advertisement
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;
daerid@gmail.com
The problem is that you are returning void. I am pretty sure you have to return an object of that type for it to work.
yeah with assignment operators you need to return *this as a reference to a constant class.

unkn.Enigma1625
unkn.Enigma1625
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?
you need to provide a copy constructor like this.

in CMyObject add

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

}
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.
I understand now. Thanks a lot for your help
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]
[ Google || Start Here || ACCU || STL || Boost || MSDN || GotW || CUJ || MSVC++ Library Fixes || BarrysWorld || [email=lektrix@barrysworld.com]E-Mail Me[/email] ]

This topic is closed to new replies.

Advertisement