为什么如果基类函数在派生类中被重载,则该函数的基类版本(即使是public)不能通过派生类的对象访问?在派生类中重载基类函数
如:
#include <iostream>
#include <string>
using namespace std;
class Base {
public:
void f(int i) {
cout << "\nInteger: " << i << endl;
}
};
class Derived : public Base {
public:
void f(string s) {
cout << "\nString: " << s << endl;
}
};
int main() {
Base b;
Derived d;
//d.f(5); Doesn't work
d.f("Hello");
//d.Base::f(5); works though
return 0;
}
[这](http://stackoverflow.com/questions/2391679/why-do-we-need-virtual-methods-in-c)可能有关系 – wowofbob 2015-04-04 21:23:52