【C++】为什么析构函数一般写成虚函数?(约232字)

由于类的多态性,基类指针可以指向派生类的对象,如果删除该基类的指针,就会调用该指针指向的派生类析构函数,而派生类的析构函数又自动调用基类的析构函数,这样整个派生类的对象完全被释放。

如果析构函数不被声明成虚函数,则编译器实施静态绑定,在删除基类指针时,只会调用基类的析构函数而不调用派生类析构函数,这样就会造成派生类对象析构不完全,造成内存泄漏。

所以将析构函数声明为虚函数是十分必要的。在实现多态时,当用基类操作派生类,在析构时防止只析构基类而不析构派生类的状况发生,要将基类的析构函数声明为虚函数。

cpp
#include 
using namespace std;

class Parent{
public:
    Parent(){
        cout << "Parent construct function"  << endl;
    };
    ~Parent(){
        cout << "Parent destructor function" <<endl;
} }; class Son : public Parent{ public: Son(){ cout << "Son construct function" << endl; }; ~Son(){ cout << "Son destructor function" <<endl;
} }; int main() { Parent* p = new Son(); delete p; p = NULL; return 0; } //运行结果: //Parent construct function //Son construct function //Parent destructor function</endl;
</endl;

将基类的析构函数声明为虚函数:

cpp
#include 
using namespace std;

class Parent{
public:
    Parent(){
        cout << "Parent construct function"  << endl;
    };
    virtual ~Parent(){
        cout << "Parent destructor function" <<endl;
} }; class Son : public Parent{ public: Son(){ cout << "Son construct function" << endl; }; ~Son(){ cout << "Son destructor function" <<endl;
} }; int main() { Parent* p = new Son(); delete p; p = NULL; return 0; } //运行结果: //Parent construct function //Son construct function //Son destructor function //Parent destructor function</endl;
</endl;

但存在一种特例,在CRTP模板中,不应该将析构函数声明为虚函数,理论上所有的父类函数都不应
该声明为虚函数,因为这种继承方式,不需要虚函数表。

update1:https://github.com/forthespada/InterviewGuide/issues/2 ,由stanleyguo0207提出 - 2021.03.22

THE END
喜欢就支持一下吧
点赞0 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容