一段简单的代码,说明了为什么析构函数要用虚函数、以及父类与子类的函数调用关系
#include<iostream>
using namespace std;
class A{
public:
A(){
cout << "A constructor called" << endl;
};
virtual ~A(){
cout << "A deconstructor called" << endl;
};
inline void funA(){
cout << "A fun called" << endl;
}
virtual void funB(){};
};
class B: public A{
public:
B(){
cout << "B constructor called" << endl;
};
~B(){
cout << "B deconstructor called" << endl;
};
inline void funB(){
cout << "B fun called" << endl;
}
};
int main()
{
A *b = new B();
b->funB();
delete b;
return 0;
}