+ 14
Can anyone explain me copy constructor in c++
3 Antworten
+ 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 :)
+ 12
SOLVED :: 😁
https://code.sololearn.com/ca960H3shede/?ref=app
+ 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";
}
};