19

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.

Community
  • 1
  • 1
Amumu
  • 17,924
  • 31
  • 84
  • 131

3 Answers3

44

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

Mysticial
  • 464,885
  • 45
  • 335
  • 332
  • So I got another question: which is the proper copy constructor function? I know const reference is the correct one, but if it is the correct one, shouldn't compiler exclude the pass by value case, since it's not the correct signature, thus no recursion should occur? – Amumu Dec 08 '11 at 19:37
  • Can you clarify on what you mean by "exclude the pass by value case"? – Mysticial Dec 08 '11 at 19:41
  • Which means it should only pick const reference as the function for copying. Every thing else is excluded. 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? – Amumu Dec 08 '11 at 19:44
  • Ah I think I see what you mean. You're saying that if you had two overloaded constructors: `complex(complex c)` and `complex(const complex &c)`... That's an interesting corner case that I'm not sure about. – Mysticial Dec 08 '11 at 19:46
10

Passing by value (rather than by reference) means a copy needs to be made. So passing by value into your copy constructor means you need to make a copy before the copy constructor is invoked, but to make a copy you first need to call the copy constructor.

Laurence Gonsalves
  • 137,896
  • 35
  • 246
  • 299
2

When a function is passed with a parameter, local variable is created corresponding to the parameter and is copied with the argument passed on to the function invocation. Hence when the function is invoked, copy constructor of the function will be invoked to copy the argument passed to the invocation to the local variable created. This results in a endless loop.

Where as when a reference is passed on to the function , local variable is not created corresponding to the parameter.

notytony
  • 1,032
  • 2
  • 11
  • 13