什么类成员函数不能成为虚函数
- 构造函数,虚函数是运行时多态,必须根据对象类型动态绑定。而调用构造函数时还不存在对象。
- static成员函数,属于一个类,而不特属于某个对象,因此不能是虚函数。
- 内联函数,内联在编译时展开,而虚函数是运行时动态绑定。
什么类成员函数必须成为虚函数
- 当一个类作为基类时,其析构函数必须是虚函数,否则可能没办法正确析构其派生类
看以下几段代码:
#include<iostream>
using namespace std;
class Base{
public:
Base() {
};
~Base() {
cout << "Output from the destructor of class Base!" << endl;};
void DoSomething() {
cout << "Do something in class Base!" <