基类析构函数加virtual;否则无法调用派生类析构函数,除非转为派生类再析构
class A
{
public:
A(){}
virtual ~A(){ cout <<“A~”<<endl;}
};
class B:public A
{
public:
B(){}
~B(){ cout <<“B~”<<endl;}
};
int main()
{
A *a = new B;
delete a;
return 0;
}
基类析构函数加virtual;否则无法调用派生类析构函数,除非转为派生类再析构
class A
{
public:
A(){}
virtual ~A(){ cout <<“A~”<<endl;}
};
class B:public A
{
public:
B(){}
~B(){ cout <<“B~”<<endl;}
};
int main()
{
A *a = new B;
delete a;
return 0;
}