Creating deep copies using references in C++

In C++, when we want to create a copy of an object, we can either create a shallow copy or a deep copy. A shallow copy simply creates a new reference to the same memory location, while a deep copy creates a completely separate copy of the object.

Deep copying using references

To create a deep copy using references in C++, we can use the copy constructor or assignment operator. The copy constructor allows us to initialize a new object with the values from an existing object, while the assignment operator allows us to assign the values from one object to another.

Here’s an example using the copy constructor:

class MyClass {
public:
    int data;

    // Copy constructor
    MyClass(const MyClass& other) {
        // Make a deep copy of the data
        data = other.data;
    }
};

int main() {
    // Create an object
    MyClass obj1;
    obj1.data = 42;

    // Create a deep copy using the copy constructor
    MyClass obj2(obj1);

    // Modify the data in the original object
    obj1.data = 24;

    // Print the data in both objects
    cout << "obj1.data: " << obj1.data << endl;  // Output: obj1.data: 24
    cout << "obj2.data: " << obj2.data << endl;  // Output: obj2.data: 42

    return 0;
}

In this example, the copy constructor of MyClass is called when obj2 is created. This constructor performs a deep copy of the data member, so modifying obj1 does not affect the value of obj2.

Using references to create deep copies ensures that the copied object has its own memory space and is independent of the original object.

#cpp #deepcopy