有时,c++ 默认允许切片可能会令人烦恼。例如
struct foo { int a; };
struct bar : foo { int b; };
int main() {
bar x{1,2};
foo y = x; // <- I dont want this to compile!
}
这个 compiles and runs as expected !不过,如果我不想启用切片怎么办?
编写 foo
的惯用方法是什么,这样就不能对任何派生类的实例进行切片?
最佳答案
我不确定它是否有一个命名习惯用法,但您可以向重载集添加一个已删除的函数,该函数比基类切片操作更匹配。如果您将 foo
更改为
struct foo
{
int a;
foo() = default; // you have to add this because of the template constructor
template<typename T>
foo(const T&) = delete; // error trying to copy anything but a foo
template<typename T>
foo& operator=(const T&) = delete; // error assigning anything else but a foo
};
那么您只能复制构造或将 foo
复制分配给 foo
。任何其他类型都会选择函数模板,并且您会收到有关使用已删除函数的错误。这确实意味着您的类和使用它的类不再是一个聚合。由于添加的成员是模板,因此它们不被视为复制构造函数或复制赋值运算符,因此您将获得默认的复制和移动构造函数和赋值运算符。
关于c++ - 防止切片的惯用方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55600025/