基类析构函数不是虚析构函数
class Base
{
public:
Base(){cout<<"base constructor"<<endl;}
~Base(){cout<<"base destructor"<<endl;}
};
class Derived:public Base
{
public:
Derived(){cout<<"derived constructor"<<endl;}
~Derived(){cout<<"derived destructor"<<endl;}
};
int main()
{
Base* p=new Base();
p=new Derived();
delete p;
return 0;
}
输出:
指针p是基类指针,即使它指向子类对象,delete指针p时,它也只调用基类析构函数
当指针就是子类指针时,delete p 会先调用子类析构函数,再调用基类析构函数,但这不是多态,因为指针本就是指向子类对象的
int main()
{
Derived * p2=new Derived();
delete p2;
return 0;
}
将基类的析构函数设为虚析构函数
class Base
{
public:
Base(){cout<<"base constructor"<<endl;}
virtual ~Base(){cout<<"base destructor"<<endl;}
};
class Derived:public Base
{
public:
Derived(){cout<<"derived constructor"<<endl;}
~Derived(){cout<<"derived destructor"<<endl;}
};
int main()
{
Base* p=new Base();
p=new Derived();
delete p;
return 0;
}
将基类的析构函数声明为虚函数之后,派生类的析构函数也自动成为虚析构函数,在主函数中基类指针p指向的是派生类对象,当delete释放p指针所指向的存储空间时,会执行派生类的析构函数,派生类的析构函数执行完之后会紧接着执行基类的析构函数
注意:
构造函数不能是虚函数,因为虚函数的入口地址在虚函数表中,虚函数表的地址在类对象的内存中,对象还未建立,更没有分配内存,此时就无法去查询虚函数表因此也就无法得知该调用哪一个构造函数了