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 writing
complex : 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.