为什么父类的析构函数定义成virtual后, delete pTest2 就会释放Derived的资源, 否则就只能释放Base的资源, 而delete pTest1就能释放Derived 和Base的资源
class Base{public: ~Base() { cout << "Base destructor!" << endl; }; };class Derived: public Base {public: ~Derived() { cout << "Derived destructor!" << endl; }; };int main() { Derived *pTest1 = new Derived(); Base *pTest2 = new Derived(); delete pTest1; delete pTest2; return 0; }
相关分类