(C++) Shallow copy

February 24, 2017 · View on GitHub

 

 

 

 

 

(C++) Shallow copy

 

A shallow copy is a type of copy operation and the opposite of a deep copy.

 

Both types of copy operations work on a class, with a pointer to data member. When this class is copied, two things can happen:

  • A shallow copy only copies the pointer, so both copies work on the same data
  • A deep copy copies the data pointed to as well, so both copies work on different data

 

The code below shows a shallow copy:

 


struct Huge {   //Huge contains much data and is hard to copy. }; struct MyClass {   //MyClass holds a pointer to a Huge.   Huge * mp_huge; }; int main() {   //My class 'a' points to a Huge.   MyClass a;   //A second class 'b' points to the same Huge:   //the Huge is _not_ copied: only the pointer pointing   //the the Huge is copied.   MyClass b = a; }