Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(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:

 

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;
}

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict