简体   繁体   English

删除复制构造函数和复制赋值运算符。 哪一项至关重要?

[英]Deleting copy constructors and copy assignment operators. Which of them are essential?

I have a use case that my object must not be copied in any way. 我有一个用例,我的对象不得以任何方式复制。 I have written an exaggerated complete list of copy constructor and copy assignment operator deletions below. 我在下面写了一个夸张的复制构造函数和复制赋值运算符删除的完整列表。 There are so many of them that I can't make sure which ones to use, and sometimes this makes me paranoid. 有这么多,我无法确定使用哪些,有时这让我变得偏执。 I don't have to write them all in my code, do I? 我不必在我的代码中全部写出来,是吗? So, in order to prevent object copying of any kind, which of them should I use? 因此,为了防止任何类型的对象复制,我应该使用哪些对象?

        MyClass             (       MyClass &)  = delete;
        MyClass             (const  MyClass &)  = delete;
        MyClass             (       MyClass &&) = delete;
        MyClass             (const  MyClass &&) = delete;
        MyClass    operator=(       MyClass &)  = delete;
        MyClass    operator=(const  MyClass &)  = delete;
const   MyClass    operator=(       MyClass &)  = delete;
const   MyClass    operator=(const  MyClass &)  = delete;
        MyClass &  operator=(       MyClass &)  = delete;
        MyClass &  operator=(const  MyClass &)  = delete;
const   MyClass &  operator=(       MyClass &)  = delete;
const   MyClass &  operator=(const  MyClass &)  = delete;
        MyClass && operator=(       MyClass &)  = delete;
        MyClass && operator=(const  MyClass &)  = delete;
const   MyClass && operator=(       MyClass &)  = delete;
const   MyClass && operator=(const  MyClass &)  = delete;
        MyClass    operator=(       MyClass &&) = delete;
        MyClass    operator=(const  MyClass &&) = delete;
const   MyClass    operator=(       MyClass &&) = delete;
const   MyClass    operator=(const  MyClass &&) = delete;
        MyClass &  operator=(       MyClass &&) = delete;
        MyClass &  operator=(const  MyClass &&) = delete;
const   MyClass &  operator=(       MyClass &&) = delete;
const   MyClass &  operator=(const  MyClass &&) = delete;
        MyClass && operator=(       MyClass &&) = delete;
        MyClass && operator=(const  MyClass &&) = delete;
const   MyClass && operator=(       MyClass &&) = delete;
const   MyClass && operator=(const  MyClass &&) = delete;

You only need to mark a single copy constructor and copy assignment operator as delete . 您只需将单个复制构造函数和复制赋值运算符标记为delete The presence of the copy versions will prevent the implicit-declaration of the move constructor and move assignment operator, and declaring one form of a copy special member function suppresses the implicit-declaration of other forms. 复制版本的存在将阻止移动构造函数和移动赋值运算符的隐式声明,并且声明一种形式的复制特殊成员函数会抑制其他形式的隐式声明。

MyClass (const MyClass&) = delete;
MyClass& operator= (const MyClass&) = delete;

Note that post-C++11, implicit-definition of the assignment operator as defaulted is deprecated and it should instead be defined as deleted. 请注意,在C ++之后的11中,赋值运算符的默认隐式定义已被弃用,而应将其定义为已删除。

copy constructor 复制构造函数

MyClass             (const  MyClass &)  = delete;

assignement operator 分配经营者

MyClass &  operator=(const  MyClass &)  = delete;

These are the only copy constructors ans copy assignement operators implicitly defined. 这些是隐式定义的唯一复制构造函数和复制分配运算符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 复制构造函数和赋值运算符 - Copy constructors and Assignment Operators 删除基类中的复制和移动构造函数/赋值运算符就足够了吗? - Is deleting copy and move constructors/assignment operators in base class enough? 删除默认C ++复制和移动构造函数和赋值运算符的缺点? - Disadvantages to deleting default C++ copy and move constructors and assignment operators? 初始化器列表:复制构造函数和赋值运算符=冗余? - Initializer lists: copy constructors and assignment operators = redundancy? 通过示例了解复制构造函数和赋值运算符 - Understanding copy constructors and assignment operators with an example 在将对象传递给函数的过程中删除了复制/移动构造函数和赋值运算符后,没有得到编译时错误 - Did not get a compile time error after deleting copy/move constructors and assignment operators during passing object to function 警告:删除它们时定义了多个复制构造函数 - Warning: Multiple copy constructors defined while deleting them 继承并删除复制和移动构造方法 - Inheritance and deleting copy and move constructors 具有多态性的复制构造函数和赋值运算符 - Copy constructor and assignment operators with polymorphism C ++-组合复制/移动运算符和构造函数 - C++ - Combining Copy/Move operators and constructors
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM