what does default operator= do?

C

cppaddict

Hi,

How, in general, does the default operator= work?

That is, say I have a custom class MyClass and do:

MyClass defaultMyClass;
MyClass initializedMyClass("set","some","members");
defaultMyClass = initializedMyClass;

Will that final line copy all of the members if initialized class into
defaultMyClass? A deep copy? If not, how does it work?

Thanks,
cpp
 
K

Karl Heinz Buchegger

cppaddict said:
Hi,

How, in general, does the default operator= work?

That is, say I have a custom class MyClass and do:

MyClass defaultMyClass;
MyClass initializedMyClass("set","some","members");
defaultMyClass = initializedMyClass;

Will that final line copy all of the members if initialized class into
defaultMyClass? A deep copy? If not, how does it work?

The default generated operator= does the only sensible thing
it can do: do a memberwise assignment.

It depends on the internals of your class if this is a deep copy
or not.
 
V

Victor Bazarov

cppaddict said:
How, in general, does the default operator= work?

In invokes operator= semantics for every base class and every member,
in the declaration order.
That is, say I have a custom class MyClass and do:

MyClass defaultMyClass;
MyClass initializedMyClass("set","some","members");
defaultMyClass = initializedMyClass;

Will that final line copy all of the members if initialized class into
defaultMyClass? A deep copy? If not, how does it work?

It's called "member-wise assignment". It all depends on the types used
and whether the assignment operators are defined to do anything different
than the default behaviour.

Also, since it has some bearing on it, read about "The Rule of Three".

Victor
 
C

cppaddict

It depends on the internals of your class if this is a deep copy

Thanks for your reply.

What does ithe deepness of the copy depend on? Eg, if one of the
members of my class is a vector which contains other custom objects,
will a deep copy of the vector be made?

Thanks,
cpp
 
P

Peter Koch Larsen

cppaddict said:
Thanks for your reply.

What does ithe deepness of the copy depend on? Eg, if one of the
members of my class is a vector which contains other custom objects,
will a deep copy of the vector be made?

Thanks,
cpp
Yes. Any standard collection will be copied correctly. Pointers are copied
too, but their content is not - and this might be problematic wrg e.g.
ownership issues.

/Peter
 
K

Karl Heinz Buchegger

cppaddict said:
Thanks for your reply.

What does ithe deepness of the copy depend on? Eg, if one of the
members of my class is a vector which contains other custom objects,
will a deep copy of the vector be made?

Yes.

As a simple rule of thumb.
If you don't need to do any special action in the destructor
to handle a specific member variable, then the copy constructor
and the assignment operator generated by the compiler will do
the right thing.

To your question: Assume there is a std::vector in your
class. You don't need to do anything in the destructor
to free that vector, because the vector handles
everything by itself. But so does it handle everythign
for itself when it is assigned to another vector.
When the compiler generates the code for op=, it uses
memberwise assignment to do this. This means that it
will tell the vector to assign itself to another vector
and since a vector knows how to do this, everything
is correct.

PS)
The above is called: rule of three
Whenever you need one of
* destructor
* copy constructor
* assignment operator
you most likely need all 3 of them
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,480
Members
44,900
Latest member
Nell636132

Latest Threads

Top