http://www.cppblog.com/aaxron/archive/2010/12/23/137293.html
一旦使用的继承机制, 如果基类没有设置为虚函数, 那么子类的析构函数很可能不会调用.
为什么是可能而不是一定呢?
因为是只会在指针的场景;
class Base
{
public:
Base(){
cout<<"Base Construcing"<<endl;
}
~Base(){
cout<<"Base Destrucing"<<endl;
}
};
class Derived : public Base
{
public:
Derived(){
cout<<"Derived Constructing"<<endl;
}
~Derived(){
cout<<"Derived Destructing"<<endl;
}
};
int main() {
Base *base = new Derived;
delete base;
cout<< "*************分割线*************"<<endl;
Derived Derived;
return 0;
}
下面是输出
Base Construcing
Derived Constructing
Base Destrucing
*************分割线*************
Base Construcing
Derived Constructing
Derived Destructing
Base Destrucing
原文:https://www.cnblogs.com/superzou/p/11892424.html