+ 14

Can anyone explain me copy constructor in c++

10th May 2017, 12:43 PM
Jay Tailor
Jay Tailor - avatar
3 Answers
+ 6
The copy constructor is used whenever a copy of an object is needed. For example, when you call a function and one of its parameters is an object (by value, not by reference), the class of that object needs a way to copy the values of the object being passed into the function. A class will already have one as a default until you specify your own. Of course, there are many other situations where the copy constructor is called , but this is just one. Does this help? happy coding :)
10th May 2017, 8:33 PM
Zeke Williams
Zeke Williams - avatar
11th May 2017, 3:26 AM
Jay Tailor
Jay Tailor - avatar
+ 3
I thought I'd add a helpful way to visualize what I said. To know if your code is calling the copy constructor simply use the following format. Class MyClass { private: //whatever you want public: MyClass (MyClass &cpy) { cout << "Calling copy constructor\n"; } };
11th May 2017, 4:02 AM
Zeke Williams
Zeke Williams - avatar