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

前端 未结 7 580
执笔经年
执笔经年 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:53

    Declaring a copy constructor private still allows member functions of the Track class to copy-construct instances of that class, while making it deleted simply forbids copy-constructing that object.

    In C++11, deleting a copy constructor is the right way to express the fact that a class is non-copyable (unless of course it makes sense for you to let member functions of Track, or friends of Track, to copy-construct Track objects).

提交回复
热议问题