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.
Example:
Syntax of Copy Constructor
Characteristics of Copy Constructor
1. The copy constructor is used to initialize the members of a newly created object by copying the members of an already existing object.
2. Copy constructor takes a reference to an object of the same class as an argument.
Sample(Sample &t) { id=t.id; }3. The process of initializing members of an object through a copy constructor is known as copy initialization.
4. 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.
5. 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.
For Example:
C++
p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 151
p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 152
p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 153
p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 154 p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 155 p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 156
p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 157
p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 158 p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 159
ID=10 ID=100ID=10 ID=101
ID=10 ID=102ID=10 ID=103 ID=10 ID=104
p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 157
ID=10 ID=106ID=10 ID=101
ID=10 ID=102ID=10 ID=109ID=10 ID=103 ID=10 ID=101ID=10 ID=103 ID=10 ID=103
ID=10 ID=102ID=10 ID=105
ID=10 ID=106ID=10 ID=107
ID=10 ID=106ID=10 ID=109
ID=10 ID=1021001 Ram 10000 1001 Ram 100001
p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 157
ID=10 ID=1021001 Ram 10000 1001 Ram 100004
ID=10 ID=102ID=10 ID=1091001 Ram 10000 1001 Ram 100007 1001 Ram 10000 1001 Ram 100008
ID=10 ID=102ID=10 ID=105
ID=10 ID=106 GFG! GFG! GFG!2
ID=10 ID=106 GFG! GFG! GFG!4
ID=10 ID=1021001 Ram 10000 1001 Ram 100001
p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 157
ID=10 ID=102ID=10 ID=103 GFG! GFG!0GFG! GFG!1 GFG! GFG!2
ID=10 ID=102ID=10 ID=103 GFG! GFG!5GFG! GFG!1 GFG! GFG!7
GFG! GFG!8
p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 157
ID=10 ID=103 GFG!1
ID=10 ID=105
ID=10 ID=102GFG!4GFG!5
ID=10 ID=102GFG!7GFG!8
p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 157
ID=10 ID=102MyClass t1, t2; MyClass t3 = t1; // ----> (1) t2 = t1; // -----> (2) 1
ID=10 ID=102MyClass t1, t2; MyClass t3 = t1; // ----> (1) t2 = t1; // -----> (2) 3MyClass t1, t2; MyClass t3 = t1; // ----> (1) t2 = t1; // -----> (2) 4 MyClass t1, t2; MyClass t3 = t1; // ----> (1) t2 = t1; // -----> (2) 5
MyClass t1, t2; MyClass t3 = t1; // ----> (1) t2 = t1; // -----> (2) 6MyClass t1, t2; MyClass t3 = t1; // ----> (1) t2 = t1; // -----> (2) 7MyClass t1, t2; MyClass t3 = t1; // ----> (1) t2 = t1; // -----> (2) 8 MyClass t1, t2; MyClass t3 = t1; // ----> (1) t2 = t1; // -----> (2) 9
ID=10 ID=102MyClass t1, t2; MyClass t3 = t1; // ----> (1) t2 = t1; // -----> (2) 3GeeksQuiz GeeksQuiz GeeksQuiz GeeksforGeeks2 GeeksQuiz GeeksQuiz GeeksQuiz GeeksforGeeks3
MyClass t1, t2; MyClass t3 = t1; // ----> (1) t2 = t1; // -----> (2) 6MyClass t1, t2; MyClass t3 = t1; // ----> (1) t2 = t1; // -----> (2) 7GeeksQuiz GeeksQuiz GeeksQuiz GeeksforGeeks6 GeeksQuiz GeeksQuiz GeeksQuiz GeeksforGeeks7
ID=10 ID=102GFG! GFG!1 p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 1500
1001 Ram 10000 1001 Ram 100001
Outputp1.x = 10, p1.y = 15 p2.x = 10, p2.y = 15
Types of Copy Constructors
1. 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++
p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 1502
p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 153
p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 154 p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 155 p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 156
p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 157
p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 158 p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 1509
ID=10 ID=102ID=10 ID=103 p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 1512
p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 157
ID=10 ID=106ID=10 ID=101
ID=10 ID=102p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 1517 p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 1518ID=10 ID=103 p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 1520
ID=10 ID=102p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 1517 p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 1523p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 1524 p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 1525
GFG! GFG!8
p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 157
ID=10 ID=103 GFG!1
ID=10 ID=105
ID=10 ID=102p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 1532
ID=10 ID=102p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 1534
ID=10 ID=102p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 1536
p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 157
ID=10 ID=102p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 1539
ID=10 ID=102p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 1541p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 1542
ID=10 ID=102p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 1544
ID=10 ID=102GFG! GFG!1 p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 1500
1001 Ram 10000 1001 Ram 100001
OutputID=10 ID=10
2. User Defined Copy Constructor
A user-defined copy constructor is generally needed when an object owns pointers or non-shareable references, such as to a file, in which case a destructor and an assignment operator should also be written
C++
p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 1549
p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 1550
p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 154 p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 155 p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 156
p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 157
p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 158 p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 1556
ID=10 ID=105
ID=10 ID=102ID=10 ID=103 p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 1512
ID=10 ID=102ID=10 ID=106ID=10 ID=101
ID=10 ID=102p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 1517 p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 1518ID=10 ID=103 p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 1568
ID=10 ID=102ID=10 ID=105
ID=10 ID=106p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 1572
ID=10 ID=102p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 1574
ID=10 ID=102p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 1576p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 1577
p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 1578
ID=10 ID=102p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 1580p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 1581
ID=10 ID=102ID=10 ID=105
ID=10 ID=106p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 1585
ID=10 ID=1021001 Ram 10000 1001 Ram 100001
ID=10 ID=102p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 1517 p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 1590
ID=10 ID=102ID=10 ID=105
ID=10 ID=106p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 1594p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 1524p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 1596
ID=10 ID=1021001 Ram 10000 1001 Ram 100001
GFG! GFG!8
ID=10 ID=103 GFG!1
ID=10 ID=105
ID=10 ID=102p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 1532
ID=10 ID=102p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 1534
ID=10 ID=102p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 1536
p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 1578
ID=10 ID=102p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 1541ID=10 ID=1012
ID=10 ID=102p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 1544
ID=10 ID=102GFG! GFG!1 p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 1500
1001 Ram 10000 1001 Ram 100001
OutputID=10 ID=10
C++
ID=10 ID=1019
p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 153
ID=10 ID=1021
p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 154 p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 155 p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 156
p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 158 ID=10 ID=1026
ID=10 ID=102ID=10 ID=103 ID=10 ID=1029
ID=10 ID=102ID=10 ID=1031
ID=10 ID=102ID=10 ID=1033 ID=10 ID=1034
p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 157
ID=10 ID=106ID=10 ID=101
ID=10 ID=102ID=10 ID=1039ID=10 ID=103ID=10 ID=1041ID=10 ID=1033ID=10 ID=1043
ID=10 ID=102ID=10 ID=1045ID=10 ID=1046
ID=10 ID=102ID=10 ID=105
ID=10 ID=106ID=10 ID=1050
ID=10 ID=106ID=10 ID=1052
ID=10 ID=106ID=10 ID=1054
ID=10 ID=1021001 Ram 10000 1001 Ram 100001
ID=10 ID=102p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 1517 ID=10 ID=1059
GFG! GFG!8
ID=10 ID=1061ID=10 ID=103 ID=10 ID=1063ID=10 ID=1033 ID=10 ID=1065
ID=10 ID=105
ID=10 ID=102ID=10 ID=1068
ID=10 ID=102ID=10 ID=1070
ID=10 ID=102ID=10 ID=1072
1001 Ram 10000 1001 Ram 100001
p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 1517 ID=10 ID=1075
ID=10 ID=105
ID=10 ID=102ID=10 ID=1078ID=10 ID=1079 ID=10 ID=1080ID=10 ID=1079 ID=10 ID=1082
1001 Ram 10000 1001 Ram 100001
ID=10 ID=103 GFG!1
ID=10 ID=105
ID=10 ID=102ID=10 ID=1088ID=10 ID=1089ID=10 ID=1090
ID=10 ID=102ID=10 ID=1092
ID=10 ID=102ID=10 ID=1094ID=10 ID=1095
ID=10 ID=102ID=10 ID=1097
ID=10 ID=102GFG! GFG!1 p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 1500
1001 Ram 10000 1001 Ram 100001
Output1001 Ram 10000 1001 Ram 10000
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++
ID=10 ID=1002
ID=10 ID=1003
p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 153
p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 154 p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 155 p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 156
p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 157
p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 158 ID=10 ID=1010
ID=10 ID=106ID=10 ID=101
ID=10 ID=102p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 1517 ID=10 ID=1015ID=10 ID=1016ID=10 ID=1017
GFG! GFG!8
p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 157
ID=10 ID=103 GFG!1
ID=10 ID=105
ID=10 ID=102ID=10 ID=1024
ID=10 ID=102ID=10 ID=1026 ID=10 ID=1027ID=10 ID=103 ID=10 ID=1029
ID=10 ID=106ID=10 ID=1031
ID=10 ID=106ID=10 ID=1033ID=10 ID=1034ID=10 ID=1035
ID=10 ID=1021001 Ram 10000 1001 Ram 100001
ID=10 ID=102GFG! GFG!1 p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 1500
1001 Ram 10000 1001 Ram 100001
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!When is a user-defined copy constructor needed?
If we don’t define our own copy constructor, the C++ compiler creates a default copy constructor for each class which does a member-wise copy between objects. The compiler-created copy constructor works fine in general. 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.
The default constructor does only shallow copy.
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 memory locations.
Copy constructor vs Assignment Operator
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. See this for more details.
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.
Example:
C++
ID=10 ID=1042
ID=10 ID=1043
ID=10 ID=1044
p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 153
p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 154 p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 155 p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 156
p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 157
p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 158 ID=10 ID=1051
ID=10 ID=100ID=10 ID=101
ID=10 ID=102ID=10 ID=1055ID=10 ID=1056
ID=10 ID=102ID=10 ID=103 ID=10 ID=1059
p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 157
ID=10 ID=106ID=10 ID=101
ID=10 ID=102ID=10 ID=10641001 Ram 10000 1001 Ram 100007 ID=10 ID=1055ID=10 ID=1067ID=10 ID=1068
ID=10 ID=102ID=10 ID=1070ID=10 ID=1071ID=10 ID=1072ID=10 ID=1073
ID=10 ID=102ID=10 ID=10641001 Ram 10000 1001 Ram 100007 ID=10 ID=1077ID=10 ID=1046
ID=10 ID=102p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 1517 ID=10 ID=1081
ID=10 ID=102ID=10 ID=105
ID=10 ID=106ID=10 ID=1085
ID=10 ID=1021001 Ram 10000 1001 Ram 100001ID=10 ID=1088
ID=10 ID=102p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 1517 ID=10 ID=10911001 Ram 10000 1001 Ram 100007 ID=10 ID=1055ID=10 ID=1094ID=10 ID=1095
GFG! GFG!8
p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 157
ID=10 ID=1098
ID=10 ID=1099
1001 Ram 10000 1001 Ram 1000000
1001 Ram 10000 1001 Ram 10000011001 Ram 10000 1001 Ram 100007 ID=10 ID=10551001 Ram 10000 1001 Ram 1000004
ID=10 ID=105
ID=10 ID=1021001 Ram 10000 1001 Ram 10000071001 Ram 10000 1001 Ram 10000081001 Ram 10000 1001 Ram 1000009
ID=10 ID=1021001 Ram 10000 1001 Ram 10000111001 Ram 10000 1001 Ram 1000012 ID=10 ID=10551001 Ram 10000 1001 Ram 1000014
ID=10 ID=1021001 Ram 10000 1001 Ram 10000161001 Ram 10000 1001 Ram 1000017
1001 Ram 10000 1001 Ram 100001
p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 157
p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 1517 1001 Ram 10000 1001 Ram 10000211001 Ram 10000 1001 Ram 100007 ID=10 ID=10551001 Ram 10000 1001 Ram 1000004
ID=10 ID=105
ID=10 ID=102ID=10 ID=10711001 Ram 10000 1001 Ram 1000028
ID=10 ID=1021001 Ram 10000 1001 Ram 10000071001 Ram 10000 1001 Ram 10000081001 Ram 10000 1001 Ram 1000009
ID=10 ID=1021001 Ram 10000 1001 Ram 10000111001 Ram 10000 1001 Ram 1000012 ID=10 ID=10551001 Ram 10000 1001 Ram 1000014
ID=10 ID=1021001 Ram 10000 1001 Ram 10000161001 Ram 10000 1001 Ram 1000017
1001 Ram 10000 1001 Ram 100001
p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 157
1001 Ram 10000 1001 Ram 10000011001 Ram 10000 1001 Ram 100007 1001 Ram 10000 1001 Ram 1000045
ID=10 ID=105
ID=10 ID=1021001 Ram 10000 1001 Ram 1000048
ID=10 ID=1021001 Ram 10000 1001 Ram 10000111001 Ram 10000 1001 Ram 1000012 ID=10 ID=10551001 Ram 10000 1001 Ram 1000014
ID=10 ID=1021001 Ram 10000 1001 Ram 10000161001 Ram 10000 1001 Ram 1000056
1001 Ram 10000 1001 Ram 100001
p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 157
ID=10 ID=103 GFG!1
ID=10 ID=105
ID=10 ID=1021001 Ram 10000 1001 Ram 10000631001 Ram 10000 1001 Ram 1000064ID=10 ID=1043
ID=10 ID=1021001 Ram 10000 1001 Ram 1000067
p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 157
ID=10 ID=1021001 Ram 10000 1001 Ram 10000701001 Ram 10000 1001 Ram 1000071
ID=10 ID=1021001 Ram 10000 1001 Ram 1000073
p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 157
ID=10 ID=1021001 Ram 10000 1001 Ram 10000761001 Ram 10000 1001 Ram 1000077ID=10 ID=1043
p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 157
ID=10 ID=1021001 Ram 10000 1001 Ram 10000701001 Ram 10000 1001 Ram 1000082
ID=10 ID=1021001 Ram 10000 1001 Ram 1000073
ID=10 ID=102GFG! GFG!1 p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 1500
1001 Ram 10000 1001 Ram 100001
OutputGeeksQuiz GeeksQuiz GeeksQuiz GeeksforGeeks
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.
C++
p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 1550
1001 Ram 10000 1001 Ram 1000090
p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 154 p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 155 p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 156
p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 157
p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 158 1001 Ram 10000 1001 Ram 1000096
ID=10 ID=105
ID=10 ID=100ID=10 ID=101
ID=10 ID=102ID=10 ID=1055 GFG! GFG! GFG!02
ID=10 ID=102ID=10 ID=103 ID=10 ID=1059
ID=10 ID=106ID=10 ID=101
ID=10 ID=102ID=10 ID=10641001 Ram 10000 1001 Ram 100007 ID=10 ID=1055 GFG! GFG! GFG!12ID=10 ID=1068
ID=10 ID=102ID=10 ID=1070ID=10 ID=1071 GFG! GFG! GFG!17ID=10 ID=1073
ID=10 ID=102p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 1517 GFG! GFG! GFG!21
ID=10 ID=102p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 1517 ID=10 ID=10911001 Ram 10000 1001 Ram 100007 ID=10 ID=1055 GFG! GFG! GFG!27ID=10 ID=1095
GFG! GFG!8
p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 157
1001 Ram 10000 1001 Ram 10000011001 Ram 10000 1001 Ram 100007 ID=10 ID=1055 GFG! GFG! GFG!34
ID=10 ID=105
ID=10 ID=1021001 Ram 10000 1001 Ram 10000071001 Ram 10000 1001 Ram 10000081001 Ram 10000 1001 Ram 1000009
ID=10 ID=1021001 Ram 10000 1001 Ram 10000111001 Ram 10000 1001 Ram 1000012 ID=10 ID=1055 GFG! GFG! GFG!44
ID=10 ID=1021001 Ram 10000 1001 Ram 10000161001 Ram 10000 1001 Ram 1000017
1001 Ram 10000 1001 Ram 100001
p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 157
ID=10 ID=1098
ID=10 ID=1099
GFG! GFG! GFG!52
p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 1517 1001 Ram 10000 1001 Ram 10000211001 Ram 10000 1001 Ram 100007 ID=10 ID=1055 GFG! GFG! GFG!34
ID=10 ID=105
ID=10 ID=102ID=10 ID=1071 1001 Ram 10000 1001 Ram 1000028
ID=10 ID=1021001 Ram 10000 1001 Ram 10000071001 Ram 10000 1001 Ram 10000081001 Ram 10000 1001 Ram 1000009
ID=10 ID=1021001 Ram 10000 1001 Ram 10000111001 Ram 10000 1001 Ram 1000012 ID=10 ID=1055 GFG! GFG! GFG!44
ID=10 ID=1021001 Ram 10000 1001 Ram 10000161001 Ram 10000 1001 Ram 1000017
1001 Ram 10000 1001 Ram 100001
p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 157
ID=10 ID=103 GFG!1
ID=10 ID=105
ID=10 ID=1021001 Ram 10000 1001 Ram 10000631001 Ram 10000 1001 Ram 1000064ID=10 ID=1043
ID=10 ID=1021001 Ram 10000 1001 Ram 1000067
p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 157
ID=10 ID=1021001 Ram 10000 1001 Ram 10000701001 Ram 10000 1001 Ram 1000071
ID=10 ID=1021001 Ram 10000 1001 Ram 1000073
p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 157
ID=10 ID=1021001 Ram 10000 1001 Ram 10000761001 Ram 10000 1001 Ram 1000077ID=10 ID=1043
p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 157
ID=10 ID=1021001 Ram 10000 1001 Ram 10000701001 Ram 10000 1001 Ram 1000082
ID=10 ID=1021001 Ram 10000 1001 Ram 1000073
ID=10 ID=102GFG! GFG!1 p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 1500
1001 Ram 10000 1001 Ram 100001
Output:
p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 150Can 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?
A copy constructor is called when an object is passed by value. Copy constructor itself is a function. So if we pass an argument by value in a copy constructor, a call to the copy constructor would be made to call the copy constructor which becomes a non-terminating chain of calls. Therefore compiler doesn’t allow parameters to be passed by value.
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?’
This article is contributed by Shubham Agrawal. If you like GeeksforGeeks and would like to contribute, you can also write your article at write.geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or if you want to share more information about the topic discussed above.