Can a object be passed as value to the copy constructor

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

提交回复
热议问题