Arguments for the copy constructor
Asked Answered
S

3

6

Why use references to the parameters of the copy constructor?

I found a lot of information saying that it is to avoid unlimited calls, but I still can't understand it.

Simmon answered 2/4, 2020 at 9:1 Comment(0)
A
7

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.

Antinucleon answered 2/4, 2020 at 9:10 Comment(3)
Also, it would defeat the purpose of the copy constructor, which is to make a copy from the original (passed by reference), and not to rely on something else to make said copy.Bordeaux
How does passing references avoid this?Simmon
Pointer passing value is also passing address, why can't pointer pass?Simmon
L
5

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.

Legitimist answered 2/4, 2020 at 9:9 Comment(0)
A
1

From this webpage

A copy constructor is called when an object is passed by value. Copy constructor itself is a function. So if we pass an argument by value in a copy constructor, a call to copy constructor would be made to call copy constructor which becomes a non-terminating chain of calls. Therefore compiler doesn’t allow parameters to be passed by value.

By passing the argument by value the copy constructor calls itself, entering in an infinite 'recursion cycle'. The link above explain pretty well the basic topics about the copy constructor.

Alleviative answered 2/4, 2020 at 9:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.