Tutorial :C++ Programming Copy Constructor [duplicate]


Possible Duplicate:
Why should the copy constructor accept its parameter by reference in C++?

Hi experts,

Can you please tell what happens if I din't give reference of an object in copy constructor.

Thanks in Advance Crazy


The correct answer is that you haven't written a copy constructor:

A non-template constructor for class X is a copy constructor if its first parameter is of type X&, const X&, volatile X& or const volatile X&, and either there are no other parameters or else all other parameters have default arguments (C++03 §12.8/2).

Why can the copy constructor's argument not be taken by value? The copy constructor is invoked to make copies of objects of the class. A copy has to be made to pass an object by value. If the copy constructor could take an object by value, you would end up with infinite recursion trying to invoke it.


It would be a recursive call to the copy constructor, as copy constructor will be invoked to create a copy of the object passed to it.

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »