6

I have some confusion with this "Can a object be passed as value to the copy constructor" The compiler clearly rejects this, which means it is not possible. Can you help me understand this?

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

Then why is it said that "Copy constructor will lead to recursive calls when pass by value is used."

James McNellis
  • 348,265
  • 75
  • 913
  • 977
Zuzu
  • 3,363
  • 5
  • 27
  • 16
  • @Kos - I disagree. People should accept *correct* answers, not their favorite. – Edward Strange Dec 08 '10 at 20:54
  • Good point! :-) That should've read "your favourite correct answers". – Kos Dec 08 '10 at 20:59
  • 2
    Well I am no specialist to judge which is the correct answer. However the one which appeals to me is most likely the one which would be selected as correct. – Zuzu Dec 09 '10 at 05:22

6 Answers6

12

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

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

Donald Miner
  • 38,889
  • 8
  • 95
  • 118
  • But the compiler does not seem to permit pass by value as a parameter for a Ctor, then where COPYing come into picture. Still cannot get this. – Zuzu Dec 08 '10 at 19:23
  • 1
    @Zuzu: That is why you cannot have a copy constructor that takes its parameter by value: it would be impossible to call because of this infinite recursion. – James McNellis Dec 08 '10 at 19:24
  • 1
    @Zuzu: *if* it was allowed, *then* there would be recursion. To prevent that hypothetical recursion, it is disallowed. – Steve Jessop Dec 08 '10 at 21:34
5

No.

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

So, if you could have such a constructor, you would need to invoke the copy constructor to copy the object you pass to the copy constructor, ad infinitum.

James McNellis
  • 348,265
  • 75
  • 913
  • 977
5

The standard specifically says (12.1/10 Constructors):

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

So it can't take a value parameter - it must be a reference.

If you think about if for a moment, it should be clear why: to pass a value parameter, the compiler must make a copy... It needs to invoke the copy constructor to do so.

Michael Burr
  • 333,147
  • 50
  • 533
  • 760
4

When a parameter is given by value, the compiler needs to copy an object first (in order create the instance which will be the argument). So for your copy constructor to be called, the compiler needs to make a copy of the object in advance.

Usually the copy constructors are defined in such a way:

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

This way, you don't ask for a separate copy of the object for the constructor, you just give a reference to an existing copy (and promise not to change that copy as well).

Vlad
  • 35,022
  • 6
  • 77
  • 199
1

Then why is it said that "Copy constructor will lead to recursive calls when pass by value is used."

Because in order to pass an object to that copy constructor by value, you might need to copy-construct it first (because the passed-by-value parameter is a temporary copy of the original object passed).

Kos
  • 70,399
  • 25
  • 169
  • 233
1

Though the answer was already given, I just wanted to clarify, since I see the same questions repeated. Dummy(Dummy dummy); is not a copy constructor. Copy constructor can either be of form Dummy(const Dummy&); or Dummy(Dummy&); The original one is clearly prohibited by the Standard.

Then you are asking:

Then why is it said that "Copy constructor will lead to recursive calls when pass by value is used."

Diagnostic message is not mandated by the standard, rather your compiler chose those words to explain why it couldn't possibly work.

Gene Bushuyev
  • 5,512
  • 20
  • 19