473,396 Members | 1,982 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

what does default operator= do?

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
5 2086
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
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
>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

"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

26
by: Steven Bethard | last post by:
I thought it might be useful to put the recent lambda threads into perspective a bit. I was wondering what lambda gets used for in "real" code, so I grepped my Python Lib directory. Here are some...
3
by: fastwings | last post by:
mm the code //////makemenu.h//// class menu { public: int op; pmenu(int op,int sub = 0) { switch op {
13
by: Peteroid | last post by:
Why does reading a member of a std::map not considered const? For example: class My_Class { int Get_Map_Value( int index ) const // ** error ** not considered const!!! { return m_Map ; //...
17
by: baibaichen | last post by:
i have written some code to verify how to disable slicing copy according C++ Gotchas item 30 the follow is my class hierarchy, and note that B is abstract class!! class B { public: explicit...
16
by: Ajay | last post by:
Hi all, i want to know when i create a class.what all it contains.I know the following things are there by default if i do not declare them by myself.Please tell the other things that are left. ...
4
by: moleskyca1 | last post by:
Hi, In a recent discussion, some of us were in disagreement about the functions the C++ compiler generates. How many functions are generated by the compiler when you declare: class Foo { };...
23
by: Jess | last post by:
Hello, I understand the default-initialization happens if we don't initialize an object explicitly. I think for an object of a class type, the value is determined by the constructor, and for...
8
by: Skybuck Flying | last post by:
Hello, Visual Studio .Net 2005 (Win32) Compile error: Error 1 error C2804: binary 'operator +' has too many parameters <snipped> line 16 class TSkybuckInt32 { private:
2
by: Peng Yu | last post by:
Hi, In the following code, the 'copy' member function works. But the '=' operator does not work. Can somebody let me know why a member function is different from an operator. Thanks, Peng ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.