class Object {
public:
...
virtual ~Object() = 0;
...
};
Object::~Object() {} // Should we always define the pure virtual destructor outside?
问题:我们是否应该始终在类定义之外定义纯虚析构函数?
换句话说,这就是我们不应该定义任何内联虚函数的原因?
谢谢
最佳答案
您可以内联定义虚函数。您不能内联定义纯虚拟函数。
根本不允许以下语法变体:
virtual ~Foo() = 0 { }
virtual ~Foo() { } = 0;
但这是完全有效的:
virtual ~Foo() { }
如果您打算实例化它或子类,则必须定义一个纯虚拟析构函数,引用 12.4/7:
A destructor can be declared virtual (10.3) or pure virtual (10.4); if any objects of that class or any derived class are created in the program, the destructor shall be defined. If a class has a base class with a virtual destructor, its destructor (whether user- or implicitly- declared) is virtual.
关于C++——我们为什么要在类定义之外定义纯虚析构函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5263524/