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

前端 未结 7 561
执笔经年
执笔经年 2021-01-01 14:16

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:38

    In the first case, you are essentially declaring a private copy constructor and then not providing any implementation. By declaring them private, non-members cannot copy it.

    In the second case, the syntax forbids a copy being made. This is C++ native.

    The major difference as a programmer is readability and understanding the code. The first case is redundant, why declare the copy constructor, make it private, and not implement it. The client has to infer a lot here.

    You can just use "= delete" and clearly imply what you're trying to do.

提交回复
热议问题