代码:
1 #include <cstdio> 2 #include <iostream> 3 4 using namespace std; 5 6 class A{ 7 public: 8 ~A(){ 9 cout<<"~A()"<<endl; 10 } 11 }; 12 13 class B:public A{ 14 public: 15 ~B(){ 16 cout<<"~B()"<<endl; 17 } 18 }; 19 20 int main(){ 21 22 A* pA = new B(); 23 delete pA; 24 cout<<"final"<<endl; 25 26 return 0; 27 }
输出:
~A() final
分析:
若将A的析构函数设置为虚函数,则输出
~B() ~A() final
所以最好将类的析构函数设为虚函数
原文:http://www.cnblogs.com/hu983/p/5522840.html