class my_class
{
...
my_class(my_class const &) = delete;
...
};
= delete
在这种情况下是什么意思?
是否有任何其他“修饰符”(除了 = 0
和 = delete
)?
原文由 Pat O‘Keefe 发布,翻译遵循 CC BY-SA 4.0 许可协议
class my_class
{
...
my_class(my_class const &) = delete;
...
};
= delete
在这种情况下是什么意思?
是否有任何其他“修饰符”(除了 = 0
和 = delete
)?
原文由 Pat O‘Keefe 发布,翻译遵循 CC BY-SA 4.0 许可协议
一个小例子来总结一些常见的用法:
class MyClass
{
public:
// Delete copy constructor:
// delete the copy constructor so you cannot copy-construct an object
// of this class from a different object of this class
MyClass(const MyClass&) = delete;
// Delete assignment operator:
// delete the `=` operator (`operator=()` class method) to disable copying
// an object of this class
MyClass& operator=(const MyClass&) = delete;
// Delete constructor with certain types you'd like to
// disallow:
// (Arbitrary example) don't allow constructing from an `int` type. Expect
// `uint64_t` instead.
MyClass(uint64_t);
MyClass(int) = delete;
// "Pure virtual" function:
// `= 0` makes this is a "pure virtual" method which *must* be overridden
// by a child class
uint32_t getVal() = 0;
}
去做:
default
和 delete
”原文由 Gabriel Staples 发布,翻译遵循 CC BY-SA 4.0 许可协议
3 回答2k 阅读✓ 已解决
2 回答3.9k 阅读✓ 已解决
2 回答3.2k 阅读✓ 已解决
1 回答3.2k 阅读✓ 已解决
1 回答2.7k 阅读✓ 已解决
3 回答3.4k 阅读
1 回答1.6k 阅读✓ 已解决
删除函数是 C++11 的一个特性: