#include <iostream>
using namespace std;
struct Base
{
virtual string get_num(string) const {return "Base";}
};
struct Derived: Base
{
// 1
string get_num(string) { return "Derived 2";} // doesn't override
// 2
//string get_num(string) const { return "Derived";} // overrides Base version
};
int main()
{
Derived d;
Base *p = &d;
cout << p->get_num("hello") << endl;
return 0;
}
仅注释2后,get_num没有覆盖基类版本,输出Base
仅注释1后,get_num覆盖基类版本,输出Derived
1,2均不注释,则1,2为重载函数成员,输出Derived