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){}
};
I already knew that the copy constructor complex(complex c)
will cause infinite recursion.
However, it should only pick const reference copy constructor complex(const complex &c)
as the function for copying, since this is the default behavior if it is not explicitly specified. Every thing else is excluded, such as complex(complex c)
.
Why does it apply the function with pass by value here? Or both are copy constructors, except the pass by value cannot be modified and is used to pass to other functions rather than its constructor?
I think in Java, it is permitted to do like that, since it will just copy the object into the constructor.