C ++中的纯虚拟析构函数

纯虚拟析构函数在C ++中是可能的。如果一个类包含纯虚拟析构函数,则必须为纯虚拟析构函数提供一个函数体。

范例程式码

#include <iostream>
using namespace std;

class B {
   public:
   virtual ~B()=0; // Pure virtual destructor
};

B::~B() {
   std::cout << "Pure virtual destructor is called";
}

class D : public B {
   public:
   ~D() {
      cout << "~D() is executed"<<endl;
   }
};

int main() {
   B *bptr=new D();
   delete bptr;
   return 0;
}

输出结果

~D() is executed
Pure virtual destructor is called