Which is the difference between declaring a constructor private and =delete?

前端 未结 7 601
醉话见心
醉话见心 2021-01-01 14:21

For example, I want to declare a class but I want the client to not be able to use the copy constructor (or copy assignment operator)

Both of the following two does

7条回答
  •  南方客
    南方客 (楼主)
    2021-01-01 14:49

    Making it private is the "old" way of doing it. The constructor still exists, but it is private, and can only be invoked from within another class member function.

    = delete deletes the constructor. It is not generated by the compiler, and it simply will not exist.

    So most likely, = delete is what you want. (although with the caveat that not all compilers support this syntax yet, so if portability is a concern...)

提交回复
热议问题