同事问了我一个问题,google 了一番后,答案指向了 Stack Overflow: https://stackoverflow.com/questions/35870081/c-base-class-function-overloading-in-a-subclass
又在老爷子的书上找了下源头:
覆盖的条件:
Override 不能跨作用域
struct Base {
void f(int);
};
struct Derived : Base {
void f(double);
};
void use(Derived d)
{
d.f(1); // call Derived::f(double)
Base& br = d
br.f(1); // call Base::f(int)
}
但是可以使用 “Using” 来改变方法的作用域,比如:
struct D2 : Base {
using Base::f; // bring all fs from Base into D2
void f(double);
};
void use2(D2 d)
{
d.f(1); // call D2::f(int), that is, Base::f(int)
Base& br = d
br.f(1); // call Base::f(int)
}
参考: