【发布时间】:2015-04-04 21:20:07
【问题描述】:
为什么如果派生类中重载了基类函数,则无法通过派生类的对象访问该函数的基类版本(即使是公共的)?
例如:
#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;
}
【问题讨论】:
-
This 可能是相关的
标签: c++