C++ Tutorial
Class and Object
Reference
Inheritance and Derivation
Polymorphism and Virtual Functions
Operator Overloading
Template
Exception
Object Oriented Advanced
Input/Output Stream
File Operations
In C++, deep copy and shallow copy are two different ways of copying the contents of one object into another. In this tutorial, we will explore both concepts and provide examples to help you understand the differences and appropriate use cases for each.
Example:
#include <iostream> class ShallowCopy { public: int *data; ShallowCopy(int value) { data = new int; *data = value; } // Shallow copy constructor ShallowCopy(const ShallowCopy& source) { data = source.data; } ~ShallowCopy() { delete data; } }; int main() { ShallowCopy obj1(10); ShallowCopy obj2(obj1); // Shallow copy std::cout << "Object 1 value: " << *obj1.data << std::endl; std::cout << "Object 2 value: " << *obj2.data << std::endl; // Modify obj2 *obj2.data = 20; std::cout << "After modifying obj2" << std::endl; std::cout << "Object 1 value: " << *obj1.data << std::endl; std::cout << "Object 2 value: " << *obj2.data << std::endl; return 0; }
Output:
Object 1 value: 10 Object 2 value: 10 After modifying obj2 Object 1 value: 20 Object 2 value: 20
Example:
#include <iostream> class DeepCopy { public: int *data; DeepCopy(int value) { data = new int; *data = value; } // Deep copy constructor DeepCopy(const DeepCopy& source) { data = new int; *data = *source.data; } ~DeepCopy() { delete data; } }; int main() { DeepCopy obj1(10); DeepCopy obj2(obj1); // Deep copy std::cout << "Object 1 value: " << *obj1.data << std::endl; std::cout << "Object 2 value: " << *obj2.data << std::endl; // Modify obj2 *obj2.data = 20; std::cout << "After modifying obj2" << std::endl; std::cout << "Object 1 value: " << *obj1.data << std::endl; std::cout << "Object 2 value: " << *obj2.data << std::endl; return 0; }
Output:
Object 1 value: 10 Object 2 value: 10 After modifying obj2 Object 1 value: 10 Object 2 value: 20
In summary, shallow copies are useful when the original and copied objects should share some or all of their data, while deep copies are appropriate when the objects should be entirely independent.
How to implement deep copy in C++:
class DeepCopyObject { public: int* dynamicData; // Deep copy constructor DeepCopyObject(const DeepCopyObject& other) : dynamicData(new int(*(other.dynamicData))) { // Constructor body } ~DeepCopyObject() { delete dynamicData; } };
Shallow copy in C++ with examples:
class ShallowCopyObject { public: int* dynamicData; // Shallow copy constructor ShallowCopyObject(const ShallowCopyObject& other) : dynamicData(other.dynamicData) { // Constructor body (no deep copying) } };
C++ std::copy for deep copy operations:
std::copy
for deep copy operations in C++, demonstrating how it can be applied to copy arrays or ranges of elements.#include <algorithm> int main() { int sourceArray[] = {1, 2, 3, 4, 5}; int destinationArray[5]; // Deep copy using std::copy std::copy(std::begin(sourceArray), std::end(sourceArray), std::begin(destinationArray)); return 0; }