+ 1
Can any one explain the difference between shallow copy and deep copy in c++?
what is meant by shallow copy and what is meant by deep copy.what are the difference between them?
1 Answer
+ 3
class A {};
class B {
A a;
};
class C {
B b;
C shallowCopy() {/*code*/}
C deepCopy() {/*code*/}
};
C c;
C d = c.shallowCopy();
C e = c.deepCopy();
A shallow copy of c creating d would make a new instance of the class C. But, both instances would share a single instance of class B.
A deep copy of c creating e would make a new instance of the class C. It would update e.b with a deep copy of c.b making a new instance of class B. It would update e.b.a with a deep copy of c.b.a making a new instance of class A.