class my_class
{
    ...
    my_class(my_class const &) = delete;
    ...
};

在这种情况下= delete是什么意思?

是否有其他“修饰符”(除了= 0和= delete)?


当前回答

这是c++ 0x标准中的新功能,可以删除继承的函数。

其他回答

本文节选自The c++ Programming Language[第4版]- Bjarne Stroustrup一书,讲述了使用=delete的真正目的:

3.3.4 Suppressing Operations Using the default copy or move for a class in a hierarchy is typically a disaster: given only a pointer to a base, we simply don’t know what members the derived class has, so we can’t know how to copy them. So, the best thing to do is usually to delete the default copy and move operations, that is, to eliminate the default definitions of those two operations: class Shape { public: Shape(const Shape&) =delete; // no copy operations Shape& operator=(const Shape&) =delete; Shape(Shape&&) =delete; // no move operations Shape& operator=(Shape&&) =delete; ˜Shape(); // ... }; Now an attempt to copy a Shape will be caught by the compiler. The =delete mechanism is general, that is, it can be used to suppress any operation

= 0表示函数是纯虚函数,不能从该类实例化对象。您需要从中派生并实现此方法 = delete意味着编译器不会为你生成这些构造函数。AFAIK,这只允许在复制构造函数和赋值操作符上。但我不太擅长即将到来的标准。

删除函数是c++ 11的一个特性:

“禁止复制”这句俗语现在可以表达出来了 直接: 类X { / /…… X& operator=(const X&) = delete;//禁止复制 X(const X&) = delete; };

[…] “删除”机制可用于任何功能。例如,我们 可以像这样消除不希望的转换: 结构Z { / /…… Z(很久);//可以初始化一个long long Z(long) = delete;//但不能少 };

新的c++ 0x标准。请参见N3242工作草案8.4.3

是否有其他“修饰符”(除了= 0和= delete)?

因为似乎没有其他人回答这个问题,我应该提到还有=default。

https://learn.microsoft.com/en-us/cpp/cpp/explicitly-defaulted-and-deleted-functions#explicitly-defaulted-functions