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
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&
orconst 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.