Arguments for the copy constructor

When you pass to a method by value, a copy is made of the argument. Copying uses the copy constructor, so you get a chicken and egg situation with infinite recursive calls to the copy constructor.

Response to comment:

Passing by reference does not make a copy of the object begin passed. It simply passes the address of the object (hidden behind the reference syntax) so the object inside the copy constructor (or any method to which an object is passed by reference) is the same object as the one outside.

As well as solving the chicken-and-egg here, passing by reference is usually (for larger objects - larger than the size of a point) faster.

Response to further comment:

You could write a kind of copy constructor that passed by pointer, and it would work in the same way as passing by reference. But it would be fiddly to call explicitly and impossible to call implicitly.

Declaration:

class X
{
public:
    X();
    X(const X* const pOther);
};

The explicit copy:

X x1;

X x2(&x1);  // Have to take address

The implicit copy:

void foo (X copyOfX);   // Pass by value, copy made

...

X x1;

foo (x1);  // Copy constructor called implicitly if correctly declared
           // But not matched if declared with pointer

foo (&x1); // Copy construcxtor with pointer might (?) be matched
           // But function call to foo isn't

Ultimately, such a thing would not be regarded as a C++ copy constructor.


This code:

class MyClass {
public:
  MyClass();
  MyClass(MyClass c);
};

does not compile. That is, because the second line here:

MyClass a;
MyClass b(a);

should theoretically cause the infinite loop you're talking about - it should construct a copy of a to before calling the constructor for b. However, if the copy constructor looks like this:

  MyClass(const MyClass& c);

Then no copies are required to be made before calling the copy constructor.