爲什麼如果基類函數在派生類中被重載,則該函數的基類版本(即使是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