-
Deep and Shallow Copy
Hi
i have looked around on the internet for some information on deep and shallow copy but cannot find any information that helps so i was wondering if any of you could help me understand them
id like to know what the difference is and how they uses the pointers to copy
any information will be greatful thanks
-
-
Suppose you have a class with a pointer member variable that should point to a dynamic array owned by the object. A shallow copy of the object would merely copy this pointer, so you would end up with two objects that have a pointer member variable pointing to the same array. A deep copy would iterate over the array and copy each element over to a new array.
The compiler generated copy constructor and copy assignment operator performs a shallow copy, so in this case you would roll out your own versions to do a deep copy. Likewise, the compiler generated destructor would only destroy the pointer, so you would provide your own destructor to correctly delete[] the dynamic array.