删除复制代码&复制分配-是公共的,私人的还是受保护的? [英] Deletion of copy-ctor & copy-assignment - public, private or protected?
问题描述
为了使对象不可复制,我们可以显式删除其复制构造函数和复制赋值运算符。
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 do it - in the public
, private
or protected
section of the class? And - does this choice make any difference?
推荐答案
在哪里做的正确选择-在该类的公共,私人或受保护的部分?
what is the right place to do it - in the public, private or protected section of the class?
我会将它们放入 public
部分。
I would put them in the public
section.
这是因为删除构造函数或赋值运算符与使它们成为 private
/ 受保护的
;并且在不删除它们的情况下,它们默认为公开
。在我看来,将删除内容放在这两个部分之一中似乎暗示着如果我不删除它们,我会将它们设为私有/受保护的,这不是您要传达的信息。
This is because deleting a constructor or an assignment operator is orthogonal to making them private
/ protected
; and when these aren't deleted, they are public
by default. Putting the deletions in one of those two sections seems to me like hinting "If I hadn't deleted them, I would have made them private/protected" - which is not a message you want to convey in your case.
不过,请注意,编译器不在乎将删除内容放入哪个部分。
Note, though, that the compiler doesn't care which section you put the deletion in.
这篇关于删除复制代码&复制分配-是公共的,私人的还是受保护的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!