By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,171 Members | 930 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,171 IT Pros & Developers. It's quick & easy.

what does default operator= do?

P: n/a
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
Jul 22 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
cppaddict wrote:

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.
--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #2

P: n/a
cppaddict wrote:
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
Jul 22 '05 #3

P: n/a
>It depends on the internals of your class if this is a deep copy
or not.


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
Jul 22 '05 #4

P: n/a

"cppaddict" <he***@hello.com> skrev i en meddelelse
news:8t********************************@4ax.com...
It depends on the internals of your class if this is a deep copy
or not.


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
Jul 22 '05 #5

P: n/a
cppaddict wrote:
It depends on the internals of your class if this is a deep copy
or not.


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

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.