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. Show
Example: Syntax of Copy Constructor Characteristics of Copy Constructor1. 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=100 ID=10 ID=101 ID=10 ID=102 ID=10 ID=103 ID=10 ID=104 p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 157 ID=10 ID=106 ID=10 ID=101 ID=10 ID=102 ID=10 ID=109 ID=10 ID=103 ID=10 ID=101 ID=10 ID=103 ID=10 ID=103 ID=10 ID=102 ID=10 ID=105 ID=10 ID=106 ID=10 ID=107 ID=10 ID=106 ID=10 ID=109 ID=10 ID=102 1001 Ram 10000 1001 Ram 100001 p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 157 ID=10 ID=102 1001 Ram 10000 1001 Ram 100004 ID=10 ID=102 ID=10 ID=109 1001 Ram 10000 1001 Ram 100007 1001 Ram 10000 1001 Ram 100008 ID=10 ID=102 ID=10 ID=105 ID=10 ID=106 GFG! GFG! GFG!2 ID=10 ID=106 GFG! GFG! GFG!4 ID=10 ID=102 1001 Ram 10000 1001 Ram 100001 p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 157 ID=10 ID=102 ID=10 ID=103 GFG! GFG!0 GFG! GFG!1 GFG! GFG!2 ID=10 ID=102 ID=10 ID=103 GFG! GFG!5 GFG! 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=102 GFG!4 GFG!5 ID=10 ID=102 GFG!7 GFG!8 p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 157 ID=10 ID=102 MyClass t1, t2; MyClass t3 = t1; // ----> (1) t2 = t1; // -----> (2)1 ID=10 ID=102 MyClass t1, t2; MyClass t3 = t1; // ----> (1) t2 = t1; // -----> (2)3 MyClass 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)6 MyClass t1, t2; MyClass t3 = t1; // ----> (1) t2 = t1; // -----> (2)7 MyClass t1, t2; MyClass t3 = t1; // ----> (1) t2 = t1; // -----> (2)8 MyClass t1, t2; MyClass t3 = t1; // ----> (1) t2 = t1; // -----> (2)9 ID=10 ID=102 MyClass t1, t2; MyClass t3 = t1; // ----> (1) t2 = t1; // -----> (2)3 GeeksQuiz GeeksQuiz GeeksQuiz GeeksforGeeks2 GeeksQuiz GeeksQuiz GeeksQuiz GeeksforGeeks3 MyClass t1, t2; MyClass t3 = t1; // ----> (1) t2 = t1; // -----> (2)6 MyClass t1, t2; MyClass t3 = t1; // ----> (1) t2 = t1; // -----> (2)7 GeeksQuiz GeeksQuiz GeeksQuiz GeeksforGeeks6 GeeksQuiz GeeksQuiz GeeksQuiz GeeksforGeeks7 ID=10 ID=102 GFG! 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 = 15 Types of Copy Constructors1. Default Copy ConstructorAn 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=102 ID=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=106 ID=10 ID=101 ID=10 ID=102 p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 1517 p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 1518 ID=10 ID=103 p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 1520 ID=10 ID=102 p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 1517 p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 1523 p1.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=102 p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 1532 ID=10 ID=102 p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 1534 ID=10 ID=102 p1.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=102 p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 1539 ID=10 ID=102 p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 1541 p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 1542 ID=10 ID=102 p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 1544 ID=10 ID=102 GFG! GFG!1 p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 1500 1001 Ram 10000 1001 Ram 100001 Output ID=10 ID=10 2. User Defined Copy ConstructorA 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=102 ID=10 ID=103 p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 1512 ID=10 ID=102 ID=10 ID=106 ID=10 ID=101 ID=10 ID=102 p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 1517 p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 1518 ID=10 ID=103 p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 1568 ID=10 ID=102 ID=10 ID=105 ID=10 ID=106 p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 1572 ID=10 ID=102 p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 1574 ID=10 ID=102 p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 1576 p1.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=102 p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 1580 p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 1581 ID=10 ID=102 ID=10 ID=105 ID=10 ID=106 p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 1585 ID=10 ID=102 1001 Ram 10000 1001 Ram 100001 ID=10 ID=102 p1.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=102 ID=10 ID=105 ID=10 ID=106 p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 1594 p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 1524 p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 1596 ID=10 ID=102 1001 Ram 10000 1001 Ram 100001 GFG! GFG!8 ID=10 ID=103 GFG!1 ID=10 ID=105 ID=10 ID=102 p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 1532 ID=10 ID=102 p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 1534 ID=10 ID=102 p1.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=102 p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 1541 ID=10 ID=1012 ID=10 ID=102 p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 1544 ID=10 ID=102 GFG! GFG!1 p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 1500 1001 Ram 10000 1001 Ram 100001 Output ID=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=102 ID=10 ID=103 ID=10 ID=1029 ID=10 ID=102 ID=10 ID=1031 ID=10 ID=102 ID=10 ID=1033 ID=10 ID=1034 p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 157 ID=10 ID=106 ID=10 ID=101 ID=10 ID=102 ID=10 ID=1039 ID=10 ID=103 ID=10 ID=1041 ID=10 ID=1033 ID=10 ID=1043 ID=10 ID=102 ID=10 ID=1045 ID=10 ID=1046 ID=10 ID=102 ID=10 ID=105 ID=10 ID=106 ID=10 ID=1050 ID=10 ID=106 ID=10 ID=1052 ID=10 ID=106 ID=10 ID=1054 ID=10 ID=102 1001 Ram 10000 1001 Ram 100001 ID=10 ID=102 p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 1517 ID=10 ID=1059 GFG! GFG!8 ID=10 ID=1061 ID=10 ID=103 ID=10 ID=1063 ID=10 ID=1033 ID=10 ID=1065 ID=10 ID=105 ID=10 ID=102 ID=10 ID=1068 ID=10 ID=102 ID=10 ID=1070 ID=10 ID=102 ID=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=102 ID=10 ID=1078 ID=10 ID=1079 ID=10 ID=1080 ID=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=102 ID=10 ID=1088 ID=10 ID=1089 ID=10 ID=1090 ID=10 ID=102 ID=10 ID=1092 ID=10 ID=102 ID=10 ID=1094 ID=10 ID=1095 ID=10 ID=102 ID=10 ID=1097 ID=10 ID=102 GFG! GFG!1 p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 1500 1001 Ram 10000 1001 Ram 100001 Output 1001 Ram 10000 1001 Ram 10000 When is the copy constructor called?In C++, a Copy Constructor may be called in the following cases:
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 ElisionIn 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=106 ID=10 ID=101 ID=10 ID=102 p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 1517 ID=10 ID=1015 ID=10 ID=1016 ID=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=102 ID=10 ID=1024 ID=10 ID=102 ID=10 ID=1026 ID=10 ID=1027 ID=10 ID=103 ID=10 ID=1029 ID=10 ID=106 ID=10 ID=1031 ID=10 ID=106 ID=10 ID=1033 ID=10 ID=1034 ID=10 ID=1035 ID=10 ID=102 1001 Ram 10000 1001 Ram 100001 ID=10 ID=102 GFG! 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 OperatorThe 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 RequiredFollowing 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=100 ID=10 ID=101 ID=10 ID=102 ID=10 ID=1055 ID=10 ID=1056 ID=10 ID=102 ID=10 ID=103 ID=10 ID=1059 p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 157 ID=10 ID=106 ID=10 ID=101 ID=10 ID=102 ID=10 ID=1064 1001 Ram 10000 1001 Ram 100007 ID=10 ID=1055 ID=10 ID=1067 ID=10 ID=1068 ID=10 ID=102 ID=10 ID=1070 ID=10 ID=1071 ID=10 ID=1072 ID=10 ID=1073 ID=10 ID=102 ID=10 ID=1064 1001 Ram 10000 1001 Ram 100007 ID=10 ID=1077 ID=10 ID=1046 ID=10 ID=102 p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 1517 ID=10 ID=1081 ID=10 ID=102 ID=10 ID=105 ID=10 ID=106 ID=10 ID=1085 ID=10 ID=102 1001 Ram 10000 1001 Ram 100001 ID=10 ID=1088 ID=10 ID=102 p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 1517 ID=10 ID=1091 1001 Ram 10000 1001 Ram 100007 ID=10 ID=1055 ID=10 ID=1094 ID=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 1000001 1001 Ram 10000 1001 Ram 100007 ID=10 ID=1055 1001 Ram 10000 1001 Ram 1000004 ID=10 ID=105 ID=10 ID=102 1001 Ram 10000 1001 Ram 1000007 1001 Ram 10000 1001 Ram 1000008 1001 Ram 10000 1001 Ram 1000009 ID=10 ID=102 1001 Ram 10000 1001 Ram 1000011 1001 Ram 10000 1001 Ram 1000012 ID=10 ID=1055 1001 Ram 10000 1001 Ram 1000014 ID=10 ID=102 1001 Ram 10000 1001 Ram 1000016 1001 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 1000021 1001 Ram 10000 1001 Ram 100007 ID=10 ID=1055 1001 Ram 10000 1001 Ram 1000004 ID=10 ID=105 ID=10 ID=102 ID=10 ID=1071 1001 Ram 10000 1001 Ram 1000028 ID=10 ID=102 1001 Ram 10000 1001 Ram 1000007 1001 Ram 10000 1001 Ram 1000008 1001 Ram 10000 1001 Ram 1000009 ID=10 ID=102 1001 Ram 10000 1001 Ram 1000011 1001 Ram 10000 1001 Ram 1000012 ID=10 ID=1055 1001 Ram 10000 1001 Ram 1000014 ID=10 ID=102 1001 Ram 10000 1001 Ram 1000016 1001 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 1000001 1001 Ram 10000 1001 Ram 100007 1001 Ram 10000 1001 Ram 1000045 ID=10 ID=105 ID=10 ID=102 1001 Ram 10000 1001 Ram 1000048 ID=10 ID=102 1001 Ram 10000 1001 Ram 1000011 1001 Ram 10000 1001 Ram 1000012 ID=10 ID=1055 1001 Ram 10000 1001 Ram 1000014 ID=10 ID=102 1001 Ram 10000 1001 Ram 1000016 1001 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=102 1001 Ram 10000 1001 Ram 1000063 1001 Ram 10000 1001 Ram 1000064 ID=10 ID=1043 ID=10 ID=102 1001 Ram 10000 1001 Ram 1000067 p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 157 ID=10 ID=102 1001 Ram 10000 1001 Ram 1000070 1001 Ram 10000 1001 Ram 1000071 ID=10 ID=102 1001 Ram 10000 1001 Ram 1000073 p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 157 ID=10 ID=102 1001 Ram 10000 1001 Ram 1000076 1001 Ram 10000 1001 Ram 1000077 ID=10 ID=1043 p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 157 ID=10 ID=102 1001 Ram 10000 1001 Ram 1000070 1001 Ram 10000 1001 Ram 1000082 ID=10 ID=102 1001 Ram 10000 1001 Ram 1000073 ID=10 ID=102 GFG! GFG!1 p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 1500 1001 Ram 10000 1001 Ram 100001 Output GeeksQuiz 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=100 ID=10 ID=101 ID=10 ID=102 ID=10 ID=1055 GFG! GFG! GFG!02 ID=10 ID=102 ID=10 ID=103 ID=10 ID=1059 ID=10 ID=106 ID=10 ID=101 ID=10 ID=102 ID=10 ID=1064 1001 Ram 10000 1001 Ram 100007 ID=10 ID=1055 GFG! GFG! GFG!12 ID=10 ID=1068 ID=10 ID=102 ID=10 ID=1070 ID=10 ID=1071 GFG! GFG! GFG!17 ID=10 ID=1073 ID=10 ID=102 p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 1517 GFG! GFG! GFG!21 ID=10 ID=102 p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 1517 ID=10 ID=1091 1001 Ram 10000 1001 Ram 100007 ID=10 ID=1055 GFG! GFG! GFG!27 ID=10 ID=1095 GFG! GFG!8 p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 157 1001 Ram 10000 1001 Ram 1000001 1001 Ram 10000 1001 Ram 100007 ID=10 ID=1055 GFG! GFG! GFG!34 ID=10 ID=105 ID=10 ID=102 1001 Ram 10000 1001 Ram 1000007 1001 Ram 10000 1001 Ram 1000008 1001 Ram 10000 1001 Ram 1000009 ID=10 ID=102 1001 Ram 10000 1001 Ram 1000011 1001 Ram 10000 1001 Ram 1000012 ID=10 ID=1055 GFG! GFG! GFG!44 ID=10 ID=102 1001 Ram 10000 1001 Ram 1000016 1001 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 1000021 1001 Ram 10000 1001 Ram 100007 ID=10 ID=1055 GFG! GFG! GFG!34 ID=10 ID=105 ID=10 ID=102 ID=10 ID=1071 1001 Ram 10000 1001 Ram 1000028 ID=10 ID=102 1001 Ram 10000 1001 Ram 1000007 1001 Ram 10000 1001 Ram 1000008 1001 Ram 10000 1001 Ram 1000009 ID=10 ID=102 1001 Ram 10000 1001 Ram 1000011 1001 Ram 10000 1001 Ram 1000012 ID=10 ID=1055 GFG! GFG! GFG!44 ID=10 ID=102 1001 Ram 10000 1001 Ram 1000016 1001 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=102 1001 Ram 10000 1001 Ram 1000063 1001 Ram 10000 1001 Ram 1000064 ID=10 ID=1043 ID=10 ID=102 1001 Ram 10000 1001 Ram 1000067 p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 157 ID=10 ID=102 1001 Ram 10000 1001 Ram 1000070 1001 Ram 10000 1001 Ram 1000071 ID=10 ID=102 1001 Ram 10000 1001 Ram 1000073 p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 157 ID=10 ID=102 1001 Ram 10000 1001 Ram 1000076 1001 Ram 10000 1001 Ram 1000077 ID=10 ID=1043 p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 157 ID=10 ID=102 1001 Ram 10000 1001 Ram 1000070 1001 Ram 10000 1001 Ram 1000082 ID=10 ID=102 1001 Ram 10000 1001 Ram 1000073 ID=10 ID=102 GFG! 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 = 150 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?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. Can you use an assignment operator (=) to copy one object to another object of a same class?You cannot use the = operator to assign one object's values to another object, unless you overload the operator.
Which of the following operators can be used to create a copy of an object?The assignment operator (operator=) is used to copy values from one object to another already existing object. The purpose of the copy constructor and the assignment operator are almost equivalent -- both copy one object to another.
Which type of function is not a member of a class but has access to the private members of the class?A friend function is a function that isn't a member of a class but has access to the class's private and protected members.
Is a special function that is called whenever a new object is created and initialized with another object's data?A copy constructor is a special constructor that is called whenever a new object is created and initialized with another object's data. ★ Sometimes the default memberwise assignment behavior in C++ is perfectly acceptable.
|