tags:

views:

107

answers:

3
+2  Q: 

Copy constructor

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?

+9  A: 

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

Amardeep
+2  A: 

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
A: 

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