Copy Constructor in C++ is used to copy one object of a class to another object.

Let go step by step to understand the concept of the copy constructor. Before starting check Constructor in C++, if you don’t know what a constructor is?

what is the Need of Copying the Object?

Consider this block of code

Notice display function in TestClass. It is accepting a TestClass object as a parameter and displaying the data of that object.

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

Because obj1 is accessible only inside the function, how it will be accessed outside the function since we are passing the object to display function.

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

Thus, the compiler automatically generates a copy of the object. But by using Copy Constructor we can inspect, observe or modify the copying of object according to our need.

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 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 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 little bit just by adding a copy constructor code.


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 this class:

This class has one pointer variable. If we create two objects of this class and try to assign/copy 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 own address space the pointer variable is pointing to address space of 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 pointer variable is created in the memory which will hold the same value and will have no link with the 1st object. This is deep copy.

That’s all for copy constructor in c++. If you have any doubt regarding copy constructor or deep/shallow copy then do comment below.

Leave a Reply