我正在尝试以下示例:
class base // base class
{
public:
std::list<base*> values;
base(){}
void initialize(base *b) {
values.push_front(b);
}
virtual ~base()
{
values.clear();
cout<<"base called"<<endl;
}
};
class derived : public base // derived class
{
public:
~derived(){
cout<<"derived called"<<endl;
}
};
int main()
{
derived *d = new derived;
base *b = new base;
b->initialize(static_cast<base *>(d)); /* filling list */
delete b;
return 0;
}
Q.1) 为什么不调用派生类的析构函数,就像我正在执行的基类析构函数一样values.clear()
?
Q.2) 如果基类析构函数是虚拟的,是否需要派生类析构函数定义?