Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Arguments for the copy constructor

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.

like image 366
yellow-monkey Avatar asked Apr 02 '20 09:04

yellow-monkey


People also ask

How many arguments does copy constructor have?

A copy constructor has one parameter that is a reference to the type that is copied.

Why should the argument of a copy constructor be const?

Why copy constructor argument should be const in C++? When we create our own copy constructor, we pass an object by reference and we generally pass it as a const reference. One reason for passing const reference is, we should use const in C++ wherever possible so that objects are not accidentally modified.

What are the features of copy constructor?

The features of copy constructor are: 1. The copy constructor should have at least one argument of the same class and this argument must be passed as a constant reference type. 2. If additional arguments are present in the copy constructor, then it must contain default arguments.

What is the advantage of copy constructor?

The Copy constructor is easier to use when our class contains a complex object with several parameters. Whenever we want to add any field to our class, then we can do so just by changing the input to the constructor. One of the most crucial importance of copy constructors is that there is no need for any typecasting.


2 Answers

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.

like image 147
Jasper Kent Avatar answered Oct 19 '22 01:10

Jasper Kent


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.

like image 30
NadavS Avatar answered Oct 19 '22 02:10

NadavS