虚析构函数 (virtual destructor) 可以帮我们实现基类指针删除派生类对象.
当我们从派生类的对象从内存中撤销时会先调用派生的析构函数, 然后再基类的析构函数, 由此就会产生问题:
Base 类:
#ifndef PROJECT6_BASE_H #define PROJECT6_BASE_H #include <iostream> using namespace std; class Base { public: Base() { cout << "执行基类构造函数" << endl; }; ~Base() { cout << "执行基类析构函数" << endl; }; }; #endif //PROJECT6_BASE_H
Derived 类:
#ifndef PROJECT6_DERIVED_H #define PROJECT6_DERIVED_H #include <iostream> #include "Base.h" using namespace std; class Derived : public Base { public: Derived() { cout << "执行派生类构造函数" << endl; }; ~Derived() { cout << "执行派生类析构函数" << endl; } }; #endif //PROJECT6_DERIVED_H
main:
#include <iostream> #include "Derived.h" using namespace std; int main() { Base *pt =new Derived; delete pt; return 0; }
输出结果:
执行基类构造函数
执行派生类构造函数
执行基类析构函数
当基类的析构函数为虚函数时, 无论指针指的是同一族中的哪一个类对象, 系统会采用动态关联, 掉啊用相应的析构函数, 对该对象进行清理工作. 即先调用了派生类的析构函数, 再调用了基类的析构函数.
Base 类:
#ifndef PROJECT6_BASE_H #define PROJECT6_BASE_H #include <iostream> using namespace std; class Base { public: Base() { cout << "执行基类构造函数" << endl; }; virtual ~Base() { cout << "执行基类析构函数" << endl; }; }; #endif //PROJECT6_BASE_H
Derived 类:
#ifndef PROJECT6_DERIVED_H #define PROJECT6_DERIVED_H #include <iostream> #include "Base.h" using namespace std; class Derived : public Base { public: Derived() { cout << "执行派生类构造函数" << endl; }; ~Derived() { cout << "执行派生类析构函数" << endl; } }; #endif //PROJECT6_DERIVED_H
main:
#include <iostream> #include "Derived.h" using namespace std; int main() { Base *pt =new Derived; delete pt; return 0; }
输出结果:
执行基类构造函数
执行派生类构造函数
执行派生类析构函数
执行基类析构函数
如果将基类的析构函数声明为虚函数时, 由该基类所派生的所有派生类的析构函数也都自动成为虚函数. 即使派生类的析构函数与其基类的构造函数名字不相同.
最好把基类的析构函数声明为虚函数. 即使基类并不需要析构函数, 我们也可以定义一个函数体为空的虚析构函数, 以保证撤销动态分配空间能正确的处理.
注: 构造函数不能声明为虚函数.