Copy Constructor in C++

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 getting passed to the display function.

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

Copy Constructor in C++

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.

Output

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

Coping of objects can be classified into two categories. Deep copy and 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 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 contructor as mentioned in the above example. In the same copy contructor you can explicitly allocate space for current object’s pointer variable and assign the value of pointer variable instead of pointing to 1st object pointer variable’s address. In this way own address space of pointer variable is created holding same value and has no link with the 1st object. This is deep copy.

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

Also check:

More C++ programs.

admin

I am a B.tech IT Student from India. Technology and programming is the most enthusiastic thing for me in this world. I like learning new techniques and use them for real-world application because I feel tech is future. Let's learn and grow TOGETHER.

Leave a Reply

Close Menu
shares