为什么父类的析构函数定义成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;
}
1 回答
- 1 回答
- 0 关注
- 558 浏览
添加回答
举报
0/150
提交
取消