In C++, a deep copy is a type of copy that creates a new object and copies all the data from the original object into the new object, including any data that is pointed to by the original object's pointers.
When you make a deep copy of an object, you create a new object with its own memory space, independent of the original object. This means that any changes made to the original object will not affect the new object, and vice versa.
A deep copy is often used when working with objects that have dynamically allocated memory, such as objects created on the heap using the new
operator. In this case, a simple copy of the object using the default copy constructor or assignment operator would result in both objects pointing to the same memory location, which can cause problems if one object is modified.
Here is an example of a deep copy:
#include <iostream> #include <string> using namespace std; class Person { public: Person(const string& name, int age); Person(const Person& other); // Copy constructor ~Person(); void introduce(); void setID(int newID); // Setter for id private: string name; int age; int* id; // Pointer to dynamically allocated memory }; Person::Person(const string& name, int age) { this->name = name; this->age = age; this->id = new int(12345); // Allocate memory for id } Person::Person(const Person& other) { this->name = other.name; this->age = other.age; this->id = new int(*other.id); // Allocate new memory for id and copy data } Person::~Person() { delete id; // Deallocate memory for id } void Person::introduce() { cout << "Hi, my name is " << name << " and I am " << age << " years old. My ID is " << *id << "." << endl; } void Person::setID(int newID) { *id = newID; // Set the value of id } int main() { Person personOne("Alice", 25); Person personTwo = personOne; // Use copy constructor to create new object p2 personOne.introduce(); personTwo.introduce(); cout << "Now lets change the ID of personTwo (pointer)" << endl; // Change ID of p1 using the setter personOne.setID(54321); personOne.introduce(); personTwo.introduce(); return 0; }
The code defines a Person
class that represents a person with a name, age, and ID. It demonstrates the use of a copy constructor and dynamic memory allocation.
In the Person
class, there are member functions including constructors, a destructor, an introduce
function, and a setter function for the ID. The ID is stored as a pointer to dynamically allocated memory.
The constructor Person::Person(const string& name, int age)
initializes a Person
object with a given name and age. It also allocates memory for the ID and assigns it a default value of 12345.
The copy constructor Person::Person(const Person& other)
creates a new Person
object by copying the values of the name and age from another Person
object. It allocates new memory for the ID and copies the value of the ID from the other object.
The destructor Person::~Person()
deallocates the memory for the ID using the delete
operator.
The introduce
function prints out the person's name, age, and ID.
The setID
function allows for changing the value of the ID by assigning a new value to the memory location pointed by id
.
In the main
function, two Person
objects, personOne
and personTwo
, are created. personTwo
is initialized using the copy constructor, which creates a new object with the same values as personOne
. The introduce
function is called for both objects to display their information.
Then, the ID of personOne
is changed using the setID
function. This demonstrates that each object has its own separate ID value stored in dynamically allocated memory. Finally, the introduce
function is called again for both objects to display their updated information.
The purpose of the code is to showcase the use of a copy constructor and dynamic memory allocation in C++.
Overall, deep copying is an important technique for working with objects that have dynamically allocated memory, and can help avoid bugs and memory leaks in C++ programs.