2

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

Why the argument to a copy constructor is passed by reference?

Community
  • 1
  • 1
anurag18294
  • 97
  • 1
  • 5
  • 13
  • 1
    think what would happen if it were passed by value... – mdma Jun 02 '10 at 14:39
  • Exact 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 Jun 02 '10 at 14:40

3 Answers3

10

If it is passed by value it would require making a copy using a...COPY CONSTRUCTOR. :-)

Amardeep AC9MF
  • 18,464
  • 5
  • 40
  • 50
3

You can't pass it by value because pass-by-value implies making/passing a copy of the thing ... making a copy of the parameter passed to the copyy constructor would be recursive, cause a stack overflow.

ChrisW
  • 54,973
  • 13
  • 116
  • 224
1

As others have mentioned you can't pass it by value - because then you'd need a copy to create a copy!

The only other alternative would be to pass by pointer, but the syntax would require address-of, like so:

MyClass copy(otherclass);  // by reference
MyClass copy(&otherclass); // by pointer
AshleysBrain
  • 22,335
  • 15
  • 88
  • 124