What is deep and shalow copy?

Answer Posted / rajk

A shallow copy of an object copies all of the member field
values. This works well if the fields are values, but may
not be what you want for fields that point to dynamically
allocated memory. The pointer will be copied. but the
memory it points to will not be copied -- the field in both
the original object and the copy will then point to the
same dynamically allocated memory, which is not usually
what you want. The default copy constructor and assignment
operator make shallow copies.

A deep copy copies all fields, and makes copies of
dynamically allocated memory pointed to by the fields. To
make a deep copy, you must write a copy constructor and
overload the assignment operator.

Deep copy needs,
If an object has pointers to dynamically allocated memory,
and the dynamically allocated memory needs to be copied
when the original object is copied, then a deep copy is
required.

A class that requires deep copies will generally need:

i )a destructor to delete the dynamically allocated memory.

ii) a copy constructor to make a copy of the dynamically
allocated memory.

iii) an overloaded assignment operator to make a copy of
the dynamically allocated memory.

Is This Answer Correct ?    20 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Can you explain polymorphism?

578


What is inheritance write a program to show use of inheritance?

611


design a c++ class for the chess board,provide a c++ class definition for such class(only class definition is required)

6143


What is basic concept of oop?

696


what are the different types of qualifier in java?

1839






There are two base class B1,B2 and there is one class D which is derived from both classes, Explain the flow of calling constructors and destructors when an object of derived class is instantiated.

1454


Which method cannot be overridden?

575


Write A Program to find the ambiguities in Multiple Inheritance? How are they resolved.(Virtual Functions)

3550


What is polymorphism explain its types?

674


Prepare me a program for the animation of train

1994


What is coupling in oop?

592


What are the 3 principles of oop?

616


i am getting an of the type can not convert int to int *. to overcome this problem what we should do?

1833


Can an interface inherit a class?

557


How does polymorphism work?

635