Summary: In this tutorial, we will learn with examples what the copy constructor is, why it is used, and how we can create a copy of an object in C++ using the same.

Introduction to Copy Constructor in C++

Copy Constructor is a parametrized constructor that initializes the copy of an object in C++.

It initializes the new object with values based on the old object.

We can define a copy constructor inside a class using the following syntax:

<ClassName> (const <ClassName>& obj){
    //code
}

The syntax is almost similar to the constructor of a class. The only difference but it accepts a constant reference of an object as an argument.

When a Copy Constructor is Called?

Whenever a copy of an object needs to be created, the copy constructor is invoked. It usually happens in the following cases:

  1. When an object is returned from a function.
  2. When an object is passed by value to a function.
  3. When an object is created using an existing object.

Consider the following program for example:

#include <iostream>;
using namespace std;
 
class TestClass{
public:
    int myVariable;
    
    //constructor
    TestClass(){
        myVariable = 2;
    }
    
    //display method
    void display(TestClass obj){
        cout << "Value of myVariable is: " << obj.myVariable;
    }
};
 
int main(){
    TestClass obj1;
    obj1.display(obj1);
}

Pay attention to the display function, it is accepting an 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, but still, it is accessible to the display function.

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

This copy created is a temporary object that is automatically generated by the C++ compiler.

Is it necessary to write a Copy Constructor?

No, it is not mandatory to write a copy constructor for every class. The compiler creates a default copy constructor in this case.

However, by defining our own copy constructor we can inspect or modify the copying of objects according to our needs.

Here is the snippet of the copy constructor of the TestClass:

TestClass (const TestClass& source){
    this->myVariable = source.myVariable;
}

The parameter is declared constant because the compiler doesn’t want us to modify the original object, not even by mistake.

Also, the object parameter is used as an alias (i.e. & in the prefix). If we don’t do so the compiler will throw an error.

Writing parameters of the copy constructor as a normal declaration (without &) means the parameter object itself is a copy. Since copy constructor is called every time a copy is created, It will stuck into an endless loop.

C++ Program using Copy Constructor

We have modified the previous example by adding a copy constructor:

#include <iostream>
using namespace std;
 
class TestClass{
public:
    int myVariable;
    
    //constructor
    TestClass(){
        myVariable = 2;
    }
    
    //Copy Constructor
    TestClass (const TestClass &source){
        this->myVariable = source.myVariable*3;
        cout << "Copy constructor called" << endl;
    }
    
    //display method
    void display(TestClass obj){
        cout << "Value of myVariable is: " << obj.myVariable;
    }
};
 
int main(){
    TestClass obj1;
    obj1.display(obj1);
}

Output:

Copy constructor called
Value of myVariable is: 6

Note the output, although we have passed the object having myVariable value as 2 to the display function, it printed 6. It is so because we have modified the value using the copy constructor.

Hence it is evident that the copy constructor is invoked every time a copy of any object is created.

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

Leave a Reply

3 × five =