Home | Projects | Notes > C++ Programming > Shallow Copy vs. Deep Copy

Shallow Copy vs. Deep Copy

 

Introduction

Consider a class that contains a pointer as a data member. Constructor allocates storage dynamically and initializes the pointer. Destructor releases the memory allocated by the constructor. What happens in the default copy constructor?

 

Default Copy Constructor (Shallow Copy)

 

User-Defined Copy Constructor (Deep Copy)

 

Examples