Deletion of copy-ctor & copy-assignment - public, private or protected?

前端 未结 5 783
感情败类
感情败类 2020-12-15 02:54

In order to make an object non-copiable we can explicitly delete both its copy-constructor and copy-assignment operator.

My question is: What is the right place to

5条回答
  •  温柔的废话
    2020-12-15 03:36

    The access of a deleted function is irrelevant. In fact, for class members, it would have made more sense to add an additional access specifier (delete:). I suspect the reason they didn't do that, was that it wouldn't work for non-member functions.

    For things like the copy constructor, it makes more sense stylistically to put it in the public section. The fact that a class doesn't have a copy constructor is a pretty major fact to know about the interface to the class.

    For internal functions where you are declaring a particular overload as deleted in order to get compiler-time detection of an error, it makes sense to declare the function in the same section as all the other overloads.

提交回复
热议问题