Can a object be passed as value to the copy constructor

后端 未结 6 1939
清歌不尽
清歌不尽 2021-01-12 18:56

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 m

相关标签:
6条回答
  • 2021-01-12 19:18

    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, ...

    0 讨论(0)
  • 2021-01-12 19:18

    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.

    0 讨论(0)
  • 2021-01-12 19:19

    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).

    0 讨论(0)
  • 2021-01-12 19:19

    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).

    0 讨论(0)
  • 2021-01-12 19:29

    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.

    0 讨论(0)
  • 2021-01-12 19:37

    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.

    0 讨论(0)
提交回复
热议问题