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

C++ConstructorCopyCopy Constructor

C++ Problem Overview


Why must a copy constructor's parameter be passed by reference?

C++ Solutions


Solution 1 - C++

Because if it's not by reference, it's by value. To do that you make a copy, and to do that you call the copy constructor. But to do that, we need to make a new value, so we call the copy constructor, and so on...

(You would have infinite recursion because "to make a copy, you need to make a copy".)

Solution 2 - C++

Because pass-by-value would invoke the copy constructor :)

Solution 3 - C++

The alternative to pass-by-reference is pass-by-value. Pass-by-value is really pass-by-copy. The copy constructor is needed to make a copy.

If you had to make a copy just to call the copy constructor, it would be a conundrum.

(I think the infinite recursion would occur in the compiler and you'd never actually get such a program.)

Besides rational reasons, it's forbidden by the standard in §12.8/3:

> A declaration of a constructor for a > class X is ill-formed if its first > parameter is of type (optionally cv- > qualified) X and either there are no > other parameters or else all other > parameters have default arguments.

Solution 4 - C++

It would be infinitely recursive if you passed it in by value

Solution 5 - C++

whenever you call a function (example: int f(car c)) which takes its arguments other than built-in data types (here car) a requirement to copy the actual object supplied by the caller to the variable in the called function's parameter.
example:

car carobj; f(carobj);

that is, copy carobj to c.

carobj needs to be copied to the parameter c in function f.

To achieve copying, the copy constructor is called.

In this case, function f called using pass by value or in other words, function f is declared to take pass by value.

If function f takes pass by reference, then its declaration is int f(car &c);

In this case,
car carobj; f(carobj);

does not need a copy constructor.

In this case, c becomes the alias of carobj.

Using the above 2 scenarios, for your clarity I am summarizing them as:

  1. If a function is declared to take a parameter as value of a object, then the copy constructor of the object is called.

  2. If a function is declared to take a parameter as "pass by reference", the parameter becomes an alias of the object supplied by the caller. No need of a copy constructor!

Now the question is why pass by reference is required. If copy constructor accepts reference, the receiving variable become aliases of supplied object. Hence, no need of copy constructor (in this case, call to itself) to copy the value in caller supplied object to copy constructor's variable in argument list.

Otherwise, if copy constructor takes the caller supplied object as value, i.e. pass by value, then it needs the copy constructor of the given object; hence, to get the supplied object from caller into our function itself (in this case the copy constructor) we need to call the copy constructor, which is nothing but calling the same function during function declaration.

That's the reason for passing a reference to a copy constructor.

Solution 6 - C++

It is necessary to pass object as reference and not by value because if you pass it by value its copy is constructed using the copy constructor.This means the copy constructor would call itself to make copy.This process will go on until the compiler runs out of memory.

Solution 7 - C++

It is very essential to pass objects as reference. If an object is passed as value to the Copy Constructor then its copy constructor would call itself, to copy the actual parameter to the formal parameter. Thus an endless chain of call to the copy constructor will be initiated. This process would go on untill the system run out of memory.

Hence, in a copy constructor, the parameter should always be passed as reference.

Solution 8 - C++

If its not passed by reference then it would pass by value. If the argument is passed by value, its copy constructor would call itself to copy the actual parameter to formal parameter. This process would go on until the system runs out of memory. So, we should pass it by reference , so that copy constructor does not get invoked.

Solution 9 - C++

A copy constructor defines what copying means,So if we pass an object only (we will be passing the copy of that object) but to create the copy we will need a copy constructor, Hence it leads to infinite recursion.

So, A copy constructor must have a reference as an argument.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJonyView Question on Stackoverflow
Solution 1 - C++GManNickGView Answer on Stackoverflow
Solution 2 - C++Brian RoachView Answer on Stackoverflow
Solution 3 - C++PotatoswatterView Answer on Stackoverflow
Solution 4 - C++Romain HippeauView Answer on Stackoverflow
Solution 5 - C++SriramanView Answer on Stackoverflow
Solution 6 - C++Anand TripathiView Answer on Stackoverflow
Solution 7 - C++Dalbir SinghView Answer on Stackoverflow
Solution 8 - C++ishaangView Answer on Stackoverflow
Solution 9 - C++newdeep jiView Answer on Stackoverflow