C++ 析构函数
C++编程思想第六版 P429
2018/8/20 继承关系中析构函数的调用顺序
#include<iostream> using std::cin; using std::cout; using std::endl; class Character { public: virtual ~Character() { cout << "Character released" << endl; } }; class NPC : public Character { public: virtual ~NPC() { cout << "NPC released" << endl; } }; int main(void) { NPC* npc = new NPC(); delete npc; cin.get(); return 0; }
运行结果
可以看出子类先释放,父对象后释放