1

Possible Duplicate:
Why should the copy constructor accept its parameter by reference in C++?

In wikipedia, it is mentioned that -

The following are invalid copy constructors (Reason - copy_from_me is not passed as reference) :

X (X copy_from_me);
X (const X copy_from_me);

because the call to those constructors would require a copy as well, which would result in an infinitely recursive call.

Could someone please explain, how it would result in an infinitely recursive call ?

Thanks.

Community
  • 1
  • 1
Mahesh
  • 34,573
  • 20
  • 89
  • 115
  • 1
    Duplicate of [Why should the copy constructor accept its parameter by reference in C++?](http://stackoverflow.com/questions/2685854/why-should-the-copy-constructor-accept-its-parameter-by-reference-in-c) – James McNellis Dec 28 '10 at 20:30
  • @James McNellis Thanks for the link. – Mahesh Dec 28 '10 at 20:45

2 Answers2

4

Both copy constructors use pass-by-value. When pass-by-value is used, a copy must be made of the argument. That copy is made by calling the copy constructor. To call the copy constructor, we need to pass by value. Which means we need to copy the argument, which means we need to call the copy constructor.....

Billy ONeal
  • 104,103
  • 58
  • 317
  • 552
2

To call a function that accepts an object by value, a copy of that value must be created and assigned to the function's stack. Since the function in question IS the method by which one would create a copy of that object, the function is called by itself in reply to it being called.

Edward Strange
  • 40,307
  • 7
  • 73
  • 125