Monday, May 23, 2016

What is Deep Copy and Shallow Copy

What is Deep Copy and Shallow Copy
The term "Deep Copy & Shallow Copy" refers to the way objects are copied.

Shallow Copy:


Shallow Copy also called "Bitwise Copy" simply copies chunk of memory from one location to another. For example memcpy().

A Shallow Copy of an object copies all members field values. It uses default copy constructor & assignment operator.

It works well with if the fields are values but not if the fields that points to dynamically allocated memory.

Deep Copy:


Deep copy also called "Member Wise Copy" copies all fields, & make copies of dynamically allocated memory pointed by the fields. To make a deep copy must write a copy constructor & overload the assignment operator.

No comments:

Post a Comment