Can a object be passed as value to the copy constructor

后端 未结 6 1940
清歌不尽
清歌不尽 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: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).

提交回复
热议问题