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

Deep Copy And Shallow Copy in C++

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.

  • Shallow Copy: A shallow copy creates a new object and copies the non-static field values of the source object into the new object. If the field is a reference or pointer type, the reference/pointer is copied (not the object being pointed to). This means that both the original and copied objects refer to the same object in memory.

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
  • Deep Copy: A deep copy creates a new object and recursively copies all the objects referenced by the source object's fields, creating separate instances of each object in memory. This ensures that the copied object is independent of the original object.

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.

  1. How to implement deep copy in C++:

    • Description: Demonstrates how to implement deep copy in C++, ensuring that the copied object has its own independent copy of dynamically allocated resources.
    • Example Code:
      class DeepCopyObject {
      public:
          int* dynamicData;
      
          // Deep copy constructor
          DeepCopyObject(const DeepCopyObject& other) : dynamicData(new int(*(other.dynamicData))) {
              // Constructor body
          }
      
          ~DeepCopyObject() {
              delete dynamicData;
          }
      };
      
  2. Shallow copy in C++ with examples:

    • Description: Illustrates shallow copy in C++, where the copied object shares the same dynamically allocated resources with the original object.
    • Example Code:
      class ShallowCopyObject {
      public:
          int* dynamicData;
      
          // Shallow copy constructor
          ShallowCopyObject(const ShallowCopyObject& other) : dynamicData(other.dynamicData) {
              // Constructor body (no deep copying)
          }
      };
      
  3. C++ std::copy for deep copy operations:

    • Description: Introduces the use of std::copy for deep copy operations in C++, demonstrating how it can be applied to copy arrays or ranges of elements.
    • Example Code:
      #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;
      }