A copy constructor is a type of constructor 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.
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.
Syntax of Copy Constructor in C++
Copy constructor takes a reference to an object of the same class as an argument:
className (const ClassName &obj)
{
member1 = obj.member1;
// for dynamic resources
......
}
Here, the const qualifier is optional but is added so that we do not modify the obj by mistake.

Syntax of Copy Constructor
For a deeper understanding of constructors and memory management, check out our Complete C++ Course, which covers constructors, destructors, and advanced object management techniques.
Examples of Copy Constructor in C++
Example 1: User Defined Copy Constructor
CPP
// C++ program to illustrate the use of copy constructor
#include <iostream>
#include <string.h>
using namespace std;
// Class definition for 'student'
class student {
int rno;
string name;
double fee;
public:
// Parameterized constructor
student(int, string, double);
// Copy constructor
student(student& t)
{
rno = t.rno;
name = t.name;
fee = t.fee;
cout << "Copy Constructor Called" << endl;
}
// Function to display student details
void display();
};
// Implementation of the parameterized constructor
student::student(int no, string n, double f)
{
rno = no;
name = n;
fee = f;
}
// Implementation of the display function
void student::display()
{
cout << rno << "\t" << name << "\t" << fee << endl;
}
int main()
{
// Create student object with parameterized constructor
student s(1001, "Manjeet", 10000);
s.display();
// Create another student object using the copy
// constructor
student manjeet(s);
manjeet.display();
return 0;
}
Output1001 Manjeet 10000
Copy Constructor Called
1001 Manjeet 10000
If the programmer does not define the copy constructor, the compiler does it for us.
Example 2: Default Copy Constructor
An implicitly defined copy constructor will copy the bases and members of an object in the same order that a constructor would initialize the bases and members of the object.
C++
// Implicit copy constructor Calling
#include <iostream>
using namespace std;
class Sample {
int id;
public:
void init(int x) { id = x; }
void display() { cout << endl << "ID=" << id; }
};
int main()
{
Sample obj1;
obj1.init(10);
obj1.display();
// Implicit Copy Constructor Calling
Sample obj2(obj1); // or obj2=obj1;
obj2.display();
return 0;
}
Need of User Defined Copy Constructor
If we don’t define our own copy constructor, the C++ compiler creates a default copy constructor for each class which works fine in general. However, we need to define our own copy constructor only if an object has pointers or any runtime allocation of the resource like a file handle, a network connection, etc because the default constructor does only shallow copy.
Shallow Copy means that only the pointers will be copied not the actual resources that the pointers are pointing to. This can lead to dangling pointers if the original object is deleted.

Deep copy is possible only with a user-defined copy constructor. In a user-defined copy constructor, we make sure that pointers (or references) of copied objects point to new copy of the dynamic resource allocated manually in the copy constructor using new operators.

Example: Class Where a Copy Constructor is Required
Following is a complete C++ program to demonstrate the use of the Copy constructor. In the following String class, we must write a copy constructor.
C++
// C++ program to demonstrate the
// Working of Copy constructor
#include <cstring>
#include <iostream>
using namespace std;
class String {
private:
char* s;
int size;
public:
String(const char* str = NULL); // constructor
~String() { delete[] s; } // destructor
String(const String&); // copy constructor
void print()
{
cout << s << endl;
} // Function to print string
void change(const char*); // Function to change
};
// In this the pointer returns the CHAR ARRAY
// in the same sequence of string object but
// with an additional null pointer '\0'
String::String(const char* str)
{
size = strlen(str);
s = new char[size + 1];
strcpy(s, str);
}
void String::change(const char* str)
{
delete[] s;
size = strlen(str);
s = new char[size + 1];
strcpy(s, str);
}
String::String(const String& old_str)
{
size = old_str.size;
s = new char[size + 1];
strcpy(s, old_str.s);
}
int main()
{
String str1("GeeksQuiz");
String str2 = str1;
str1.print(); // what is printed ?
str2.print();
str2.change("GeeksforGeeks");
str1.print(); // what is printed now ?
str2.print();
return 0;
}
OutputGeeksQuiz
GeeksQuiz
GeeksQuiz
GeeksforGeeks
Note: Such classes also need the overloaded assignment operator. See this article for more info – C++ Assignment Operator Overloading
What would be the problem if we remove the copy constructor from the above code?
If we remove the copy constructor from the above program, we don’t get the expected output. The changes made to str2 reflect in str1 as well which is never expected. Also, if the str1 is destroyed, the str2’s data member s will be pointing to the deallocated memory.
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).
Refer to this article for more details – When is a Copy Constructor Called in C++?
In copy elision, the compiler prevents the making of extra copies by making the use to techniques such as NRVO and RVO which results in saving space and better the program complexity (both time and space); Hence making the code more optimized.
The main difference between Copy Constructor and Assignment Operator is that the Copy constructor makes a new memory storage every time it is called while the assignment operator does not make new memory storage.
Which of the following two statements calls the copy constructor and which one calls the assignment operator?
MyClass t1, t2;
MyClass t3 = t1; // ----> (1)
t2 = t1; // -----> (2)
A copy constructor is called when a new object is created from an existing object, as a copy of the existing object. The assignment operator is called when an already initialized object is assigned a new value from another existing object. In the above example (1) calls the copy constructor and (2) calls the assignment operator.
Frequently Asked Questions in C++ Copy Constructors
Can we make the copy constructor private?
Yes, a copy constructor can be made private. When we make a copy constructor private in a class, objects of that class become non-copyable. This is particularly useful when our class has pointers or dynamically allocated resources. In such situations, we can either write our own copy constructor like the above String example or make a private copy constructor so that users get compiler errors rather than surprises at runtime.
Why argument to a copy constructor must be passed as a reference?
If you pass the object by value in the copy constructor, it will result in a recursive call to the copy constructor itself. This happens because passing by value involves making a copy, and making a copy involves calling the copy constructor, leading to an infinite recursion. Using a reference avoids this recursion. So, we use reference of objects to avoid infinite calls.
Why argument to a copy constructor should be const?
One reason for passing const reference is, that we should use const in C++ wherever possible so that objects are not accidentally modified. This is one good reason for passing reference as const, but there is more to it than ‘Why argument to a copy constructor should be const?’
Related Articles: