Shallow Copy Vs Deep Copy

Can anyone explain the difference between shallow copy and deep copy in c++. I studied that but i don't have clear idea about these two.
With a deep copy, if the thing you're copying has a pointer to some data, you make a copy of that data as well.

With a shallow copy, if the thing you're copying has a pointer to some data, you don't make a copy of that data and both objects (the original and the copy) share that same data (so changing the data in one object also changes it in the other, because they're both using that exact same data).
Shallow copying is creating a new object and then copying the non static fields of the current object to the new object. If the field is a value type, a bit by bit copy of the field is performed. If the field is a reference type, the reference is copied but the referred object is not, therefore the original object and its clone refer to the same object. A shallow copy of an object is a new object whose instance variables are identical to the old object. In .Net shallow copy is done by the object method MemberwiseClone().

Deep copy is creating a new object and then copying the non-static fields of the current object to the new object. If a field is a value type, a bit by bit copy of the field is performed. If a field is a reference type, a new copy of the referred object is performed. A deep copy of an object is a new object with entirely new instance variables, it does not share objects with the old. While performing Deep Copy the classes to be cloned must be flagged as [Serializable].

More about....Shallow copy and Deep copy...

http://net-informations.com/faq/net/shallow-deep-copy.htm

Balmer
Topic archived. No new replies allowed.