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

is there any choice in opting subjects like 4 out of 7

1733


What is overloading in oop?

579


What is a null tree?

632


Why is polymorphism used?

588


write a program to find 2^n+1 ?

1555






What is object and example?

606


when to use 'mutable' keyword and when to use 'const cast' in c++

1652


What are properties in oop?

613


What is object-oriented programming? Webopedia definition

729


Why oops is important?

616


What is polymorphism oop?

625


What does <> mean pseudocode?

626


Why interface is used?

555


What is polymorphism in oops?

565


Why do we use oop?

607