What is deep and shalow copy?

Answer Posted / hemanth

Deep Copy and Shallow Copy

The terms "deep copy" and "shallow copy" refer to the way
objects are copied, for example, during the invocation of a
copy constructor or assignment operator. In a deep copy
(also called "memberwise copy"), the copy operation
respects object semantics. For example, copying an object
that has a member of type std::string ensures that the
corresponding std::string in the target object is copy-
constructed by the copy constructor of class std::string.


class A
{
string s;
};
A a;
A b;
a=b; //deep copy
When assigning b to a, the compiler-generated assignment
operator of class A first invokes the assignment operator
of class std::string. Thus, a.s and b.s are well-defined,
and they are probably not binary-identical. On the other
hand, a shallow copy (also called "bitwise copy") simply
copies chunks of memory from one location to another. A
memcpy() operation is an example of a shallow copy. Because
memcpy() does not respect object semantics, it will not
invoke the copy constructor of an object. Therefore, you
should never use memcpy() to copy objects. Use it only when
copying POD (Plain Old Data) types: ints, floating point
numbers, and dumb structs.

Is This Answer Correct ?    4 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is polymorphism in oops?

556


i=20;k=0; for(j=1;k-i;k+=j<10?4:3) { cout<

1413


What are the benefits of interface?

577


can we make game by using c

3416


Why is destructor used?

581






What is balance factor?

580


What is class in oop with example?

616


just right the logic of it 1--> If few people are electing then every time ur candidate should win 2--> arrange books in box, if box carry weight == books weight then take another box..... find the no of box required.

6483


What is polymorphism what are the different types of polymorphism?

561


What is difference between pop and oop?

601


I have One image (means a group photo ) how to split the faces only from the image?............ please send the answer nagadurgaraju@gmail.com thanks in advace...

1622


Write a program to sort the number with different sorts in one program ??

1914


Why do we use encapsulation in oops?

518


What is object and class in oops?

586


What is abstraction in oop with example?

642