Possible Duplicate:
Why should the copy constructor accept its parameter by reference in C++?
Can a object be passed as value to the copy constructor
Consider this piece of code:
class complex{
private:
double re, im;
public:
complex(double _re, double _im):re(_re),im(_im){}
complex(complex c):re(c.re),im(c.im){}
};
When compiled, I got an error message: invalid constructor; you probably meant ‘complex (const complex&)’
In the book C++ Programming Language, it is written that:
The copy constructor defines what copying means – including what
copying an argument means – so writingcomplex : complex(complex c) :re(c.re) , im(c.im) { } // error
is an error because any call would have involved an infinite recursion.
Why does this cause infinite recursion? It doesn’t make sense.
Passing by value means that the parameter is copied into the function. That calls the copy constructor.
If your copy constructor parameter is pass-by-value… It would call itself… over and over again…