虚拟函数的引入是为了实现多态性(polymorphism),而虚拟析构函数是怎么实现多态性的?我想通过以下例子进行说明。
#include <iostream>
using namespace std;
class employee{
public:
employee(){
cout << "employee construction" << endl;
}
~employee(){
cout << "employee destruction" << endl;
}
};
class programmer: public employee{
public:
programmer(){
cout << "programmer construction" << endl;
}
~programmer(){
cout << "programmer destruction" << endl;
}
};
void main(){
{
programmer pg;
}
cout << "---------------------------" << endl;
{
employee *pe = new programmer( );
delete pe;
}
}
output:
employee construction
programmer construction
programmer destruction
employee destruction
---------------------------
employee construction
programmer construction
employee destruction