When is the copy constructor called?

In C++, a Copy Constructor may be called in the following cases: 

  • When an object of the class is returned by value. 
  • When an object of the class is passed (to a function) by value as an argument. 
  • When an object is constructed based on another object of the same class. 
  • When the compiler generates a temporary object.

It is, however, not guaranteed that a copy constructor will be called in all these cases, because the C++ Standard allows the compiler to optimize the copy away in certain cases, one example is the return value optimization (sometimes referred to as RVO).

Copy Elision

In copy elision, the compiler prevents the making of extra copies which results in saving space and better the program complexity(both time and space); Hence making the code more optimized.  

Example:

C++




// C++ program to demonstrate
// the working of copy elision
#include <iostream>
using namespace std;
 
class GFG {
public:
    void print() { cout << " GFG!"; }
};
 
int main()
{
    GFG G;
    for (int i = 0; i <= 2; i++) {
        G.print();
        cout << "\n";
    }
    return 0;
}


Output

 GFG!
 GFG!
 GFG!

Now it is on the compiler to decide what it wants to print, it could either print the above output or it could print case 1 or case 2 below, and this is what Return Value Optimization is. In simple words, RVO is a technique that gives the compiler some additional power to terminate the temporary object created which results in changing the observable behavior/characteristics of the final program.

Case 1:

GFG!
GFG!

Case 2:

GFG!

Copy Constructor in C++

Pre-requisite: Constructor in C++ 

A copy constructor is a member function that initializes an object using another object of the same class. In simple terms, a constructor which creates an object by initializing it with an object of the same class, which has been created previously is known as a copy constructor.  

Copy constructor is used to initialize the members of a newly created object by copying the members of an already existing object.

Copy constructor takes a reference to an object of the same class as an argument.

Sample(Sample &t)
{
    id=t.id;
}

The process of initializing members of an object through a copy constructor is known as copy initialization.

It is also called member-wise initialization because the copy constructor initializes one object with the existing object, both belonging to the same class on a member by member copy basis.

The copy constructor can be defined explicitly by the programmer. If the programmer does not define the copy constructor, the compiler does it for us.
 

Example:

Syntax of Copy Constructor

C++




#include <iostream>
#include <string.h>
using namespace std;
class student {
    int rno;
    char name[50];
    double fee;
 
public:
    student(int, char[], double);
    student(student& t) // copy constructor
    {
        rno = t.rno;
        strcpy(name, t.name);
        fee = t.fee;
    }
    void display();
};
 
student::student(int no, char n[], double f)
{
    rno = no;
    strcpy(name, n);
    fee = f;
}
 
void student::display()
{
    cout << endl << rno << "\t" << name << "\t" << fee;
}
 
int main()
{
    student s(1001, "Manjeet", 10000);
    s.display();
 
    student manjeet(s); // copy constructor called
    manjeet.display();
 
    return 0;
}


Output

1001    Manjeet    10000
1001    Manjeet    10000

C++




#include <iostream>
#include <string.h>
using namespace std;
class student {
    int rno;
    char name[50];
    double fee;
 
public:
    student(int, char[], double);
    student(student& t) // copy constructor (member wise
                        // initialization)
    {
        rno = t.rno;
        strcpy(name, t.name);
    }
    void display();
    void disp() { cout << endl << rno << "\t" << name; }
};
student::student(int no, char n[], double f)
{
    rno = no;
    strcpy(name, n);
    fee = f;
}
 
void student::display()
{
    cout << endl << rno << "\t" << name << "\t" << fee;
}
 
int main()
{
    student s(1001, "Manjeet", 10000);
    s.display();
 
    student manjeet(s); // copy constructor called
    manjeet.disp();
 
    return 0;
}


Output

1001    Manjeet    10000
1001    Manjeet

Similar Reads

Characteristics of Copy Constructor

...

Types of Copy Constructors

...

When is the copy constructor called?

1. The copy constructor is used to initialize the members of a newly created object by copying the members of an already existing object....

When is a user-defined copy constructor needed?

...

Copy constructor vs Assignment Operator

1. Default Copy Constructor...

Example – Class Where a Copy Constructor is Required

...

Can we make the copy constructor private?

...

Why argument to a copy constructor must be passed as a reference?

...

Why argument to a copy constructor should be const?

In C++, a Copy Constructor may be called in the following cases:...