Copy Constructor in C++ is used to create a copy of the object of its class.

Recommended: Constructor in C++

What is the Need of Copying the Object?

Consider this block of C++ code.

Observe the display function of TestClass. It is accepting a TestClass object as a parameter and displaying the data of the same.

Since the object obj1 is declared inside the main function, the scope of the obj1 is limited to the main function.

how the obj1 is accessible in the display function as it is no scope outside the main function?

It is because instead of the original object, the copy of obj1 is being passed to the display function.

The compiler automatically generates a copy of the object.

Using Copy Constructor we can inspect, observe, or modify the copying of objects according to our needs.

Here is the snippet of the copy constructor of TestClass.

The copy constructor is more or less the same as the constructor of the class. The only difference is that it accepts an object as a parameter.

Here the object argument source is declared constant because we do not want to make changes to the original object even by mistake.

Also, we are using it as an alias (i.e. & in the prefix) because if we use it as a normal object then it would be a copy of the original object, the funny thing is that the copy constructor is the only one which does the copy job for the object.

Hence if we don’t use alias we would get stuck in an endless loop (i.e. copy constructor calling itself again and again).

Program illustrating copy constructor in C++

We have changed the above program a little bit by adding a copy constructor.


Copy constructor in c++

Notice in the output before getting the output of display function, we are getting the output of copy constructor, although we haven’t called Copy constructor explicitly.

It proves that copy constructor is getting called automatically to create the copy of obj1 for display function (i.e. argument of display function is the copy of original object obj1).

Shallow Copy and Deep Copy

Copying of objects can be classified into two categories.

  • Deep Copy.
  • Shallow Copy.

Consider the following class in C++:

This class has one pointer variable i.e. myVariable.

If we create two objects of this class and try to assign one object to another then it may happen that the pointer variable of the 2nd object will store the address of the pointer variable of the 1st object.

This is known as shallow copy because instead of creating its own address space the pointer variable is pointing to the address space of the old object (1st object).

But you can explicitly make your own copy constructor as mentioned in the above example.

In the same copy constructor, you can explicitly allocate space for the current object’s pointer variable and assign the value of the pointer variable instead of pointing to 1st object pointer variable’s address.

In this way own address space of the pointer variable is created in the memory which will hold the same value and will have no link with the 1st object. This is the deep copy.

In this tutorial, we learned about copy constructor in C++.

Leave a Reply