A copy constructor is a special type of constructor in C++ that creates an object by copying an existing object. The copy constructor is used to initialize an object with the values of another object of the same class.
In C++, the copy constructor is declared with the following syntax:
ClassName(const ClassName& obj)
C++Here, ClassName
is the name of the class, and obj
is the reference to the object that is being copied. The copy constructor is usually defined in the class definition.
How Copy Constructor Works
When an object is created using the copy constructor, a new object is created with the same values as the original object. The copy constructor copies the values of all non-static data members from the original object to the new object.
class Example {
public:
// Constructor
Example(int value) {
m_value = value;
}
// Copy Constructor
Example(const Example& other) {
m_value = other.m_value;
}
private:
int m_value;
};
C++In the above example, the copy constructor creates a new object with the same value as the original object. The m_value
variable is copied from the original object to the new object.
When Copy Constructor is Called
The copy constructor is called in the following scenarios:
- When an object is created using another object of the same class.
- When an object is passed as a value to a function.
- When an object is returned by value from a function.
- When an object is used to initialize another object of the same class.
void func(Example obj) {
// Do something
}
int main() {
Example obj1(10); // Constructor is called
Example obj2 = obj1; // Copy constructor is called
Example obj3(obj1); // Copy constructor is called
func(obj1); // Copy constructor is called
return 0;
}
C++In the above example, the copy constructor is called when the obj2
, obj3
, and func
are created.
Default Copy Constructor
If a copy constructor is not defined in a class, the compiler creates a default copy constructor. The default copy constructor creates a new object with the same values as the original object. However, it only copies the values of the non-static data members.
class Example {
public:
// Constructor
Example(int value) {
m_value = value;
}
private:
int m_value;
};
C++In the above example, the compiler creates a default copy constructor that copies the m_value
variable from the original object to the new object.
Copy Constructor and Shallow Copy
When the copy constructor is used to copy an object that contains pointers or dynamic memory allocations, a shallow copy is performed. A shallow copy copies the value of the pointer, not the data that the pointer points to.
class Example {
public:
// Constructor
Example(int size) {
m_size = size;
m_data = new int[m_size];
}
// Copy Constructor
Example(const Example& other) {
m_size = other.m_size;
m_data = other.m_data; // Shallow Copy
}
private:
int m_size;
int* m_data;
};
C++In the above example, the copy constructor performs a shallow copy of the m_data
variable. This means that both the original object and the new object point to the same memory location, and any changes made to the m_data
array of one object will also affect the other object.
Copy Constructor and Deep Copy
To avoid the problems caused by shallow copy, a deep copy can be performed. A deep copy creates a new memory location for the copied data, and copies the data pointed to by the pointer.
class Example {
public:
// Constructor
Example(int size) {
m_size = size;
m_data = new int[m_size];
}
// Copy Constructor
Example(const Example& other) {
m_size = other.m_size;
m_data = new int[m_size]; // Deep Copy
for (int i = 0; i < m_size; i++) {
m_data[i] = other.m_data[i];
}
}
// Destructor
~Example() {
delete[] m_data;
}
private:
int m_size;
int* m_data;
};
C++In the above example, the copy constructor performs a deep copy of the m_data
variable. This means that a new memory location is created for the new object’s m_data
array, and the data pointed to by the pointer is copied. Any changes made to the m_data
array of one object will not affect the other object.
Copy Constructor and Assignment Operator
The copy constructor is related to the assignment operator (=
). When an object is assigned to another object using the =
operator, the copy constructor is called to create a new object with the values of the original object.
Example obj1(10); // Constructor is called
Example obj2(obj1); // Copy constructor is called
Example obj3 = obj1; // Copy constructor is called
C++In the above example, the copy constructor is called when obj2
and obj3
are created, and the =
operator is used to assign obj1
to obj3
.
Conclusion
In C++, the copy constructor is used to create an object by copying an existing object. It is important to define a copy constructor when a class contains pointers or dynamic memory allocations. Shallow copy can lead to problems if not handled properly, and deep copy should be used to ensure that each object has its own memory space.