Can an object be passed as a value to the copy constructor

I have some confusion with this: “Can an object be passed as a value to the copy constructor” The compiler explicitly rejects this, which means that this is not possible. Can you help me figure this out?

class Dummy{ Dummy(Dummy dummy){ // This is not possible } }; 

Then why is it said that "Copying the constructor will result in recursive calls when using pass by value".

+6
c ++
source share
6 answers

This is because in order to pass by value, you need to COPY an object. This way you pass a copy of the object in the definition of how to copy it.

Effectively, what really happens if this trap is not there, your copy constructor will call your copy constructor to create a copy, which will call your copy constructor to create a copy that will call your copy constructor to create a copy, ...

+12
source share

Not.

As the error says, this will result in recursive calls to the copy constructor. The copy constructor is used to copy an object. You need to make a copy of the object when it is passed by value.

So, if you can have such a constructor, you will need to call the copy constructor to copy the object that you pass to the copy constructor, to infinity.

+5
source share

The standard specifically states (12.1 / 10 Constructors):

The copy constructor for class X is a constructor with the first parameter of type X & or of type const X &

Therefore, it cannot accept a value parameter - it must be a reference.

If you think for a moment, it should be clear why: to pass a value parameter, the compiler must make a copy ... To do this, call the copy constructor.

+5
source share

When a parameter is set by value, the compiler must first copy the object (to create an instance that will be an argument). Therefore, for your calling copy constructor, the compiler must make a copy of the object in advance.

Typically, copy constructors are defined this way:

 Dummy(const Dummy& dummy) { // This is possible } 

Thus, you do not request a separate copy of the object for the constructor, you just give a link to the existing copy (and promise not to change this copy).

+4
source share

Then why is it said that "Copying the constructor will result in recursive calls when using pass by value".

Since in order to pass an object to this copy constructor by value, you may need to copy-build it first (since the pass-by-value parameter is a temporary copy of the original object that passed).

+1
source share

Although the answer has already been given, I just wanted to clarify, since I see that the same questions are repeated. Dummy(Dummy dummy); not a copy constructor. The copy constructor can either be a Dummy(const Dummy&); form Dummy(const Dummy&); or Dummy(Dummy&); . The original is expressly prohibited by the Standard.

Then you ask:

Then why is it said that "Copy constructor will result in recursive calls when a value bypass is used."

The diagnostic message is not standard, rather, your compiler chose these words to explain why it cannot work.

+1
source share

All Articles