本文的主要参考书籍是 C++ Primer
struct A {
virtual ~A() = 0;
};
struct B : A {
virual ~B() {}
};
int main( void ) {
B x;
}
看看下面的解释:
A destructor can be declared virtual(10.3) or pure virtual(10.4);if any object 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.
struct A
{
virtual ~A() = 0 {}
};
应当写成
struct A
{
virtual ~A() = 0;
};
A::~A()
{
}
因为C++规定 =0 和 {} 不能同时出现。
[Note: a function declaration cannot provide both a pure-specifier and a definition
—end note]
[Example:
struct C {
virtual void f() = 0 { }; // ill-formed
};
—end example]
必须实现虚析构函数 ,纯虚析构函数的原因 大致如上!