copy constructor and assignment operator

H

harry

Hi,
what is the difference between copy constructor and assignment
operator?
and tell me the difference in terms of deep copy and shallow copy..
and which one refers deep copy and shallow copy..

thanks
 
H

harry

Hi,
what is the difference between copy constructor and assignment
operator?
and tell me the difference in terms of deep copy and shallow copy..
and which one refers deep copy and shallow copy..

thanks

As i know that copy constructor copies a existing object to a non
existing object, which you are going to create. and Assignment
operation can happen between two existing objects.
but i have confusion that which one referring shallow copy and deep
copy.
 
V

Vladimir Jovic

harry said:
As i know that copy constructor copies a existing object to a non
existing object, which you are going to create. and Assignment
operation can happen between two existing objects.
but i have confusion that which one referring shallow copy and deep
copy.

That depends how you implement your copy constructor and operator=

This might help:
http://lmgtfy.com/?q=c+++shallow+vs+deep+copy
 
V

Victor Bazarov

harry said:
As i know that copy constructor copies a existing object to a non
existing object, which you are going to create. and Assignment
operation can happen between two existing objects.
but i have confusion that which one referring shallow copy and deep
copy.

There is no "shallow copy" or "deep copy" as far as copying is
concerned. The default behaviour of the copy-constructor is to
copy-construct all members, following individual member type's
copy-construction semantics. The default behaviour of the copy
assignment operator is to copy-assign all members following the copy
assignment semantics for every individual member type. Array members
(while arrays in C++ have no copy assignment or copy construction
defined for them when they are stand-alone) are still constructed or
assigned element by element (again, following the semantics for the type
of the element). Those semantics *nest*.

V
 
G

Gil

Hi,
what is the difference between copy constructor and assignment
operator?
and tell me the difference in terms of deep copy and shallow copy..
and which one refers deep copy and shallow copy..

thanks

obviously copy constructor is a constructor. that means it deals with
uninitialized non-static data members. it has to assign values to
them.

assignment operator works with fully constructed objects and thus it
must handle resource allocation/deallocation and the self assignment
situations.

'shallow/deep copy' are just programming terms (not specific to C/C++)
to distinguish between how resources are copied:
-shallow: handles to resources are copied (resources become shared)
-deep: resources are duplicated

the compiler auto-generated copy constructor and assignment operator
perform a 'shallow' copy.

gil
 
S

SG

'shallow/deep copy' are just programming terms (not specific to
C/C++) to distinguish between how resources are copied:
-shallow: handles to resources are copied (resources become shared)
-deep: resources are duplicated

the compiler auto-generated copy constructor and assignment operator
perform a 'shallow' copy.

No. The compiler generated copy constructors and assignment operators
just copy and assign member-wise. If you have a vector as a member the
vector is properly copied ("duplicated", no sharing of its data).

Cheers!
SG
 
R

Ron

obviously copy constructor is a constructor. that means it deals with
uninitialized non-static data members.

This is the essence!
it has to assign values to
them.

This is INCORRECT generally. A proper constructor (copy or otherwise)
INITIALIZES
the members. It is only in the less fortunate cases that it has to
do this via
assignment (and only due to woeful decisions made early on in the C++
history
that it ever does this to uninitialized values).
 

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,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top