今天我觉得自己像个菜鸟:
class Base
{
public:
virtual void foo(int)=0;
virtual void foo(int, int) {}
virtual void bar() {}
};
class Derived : public Base
{
public:
virtual void foo(int) {}
};
void main()
{
Derived d;
d.bar(); // works
d.foo(1); // works
d.foo(1,2); // compiler error: no matching function call
}我期望d从Base继承foo(int, int),但事实并非如此。那么我在这里错过了什么呢?
发布于 2012-08-25 00:19:35
这是因为具有相同名称的基函数是隐藏的。
对于没有覆盖的函数,您需要使用using:
class Derived : public Base
{
public:
using Base::foo;
virtual void foo(int) {} //this hides all base methods called foo
};https://stackoverflow.com/questions/12113074
复制相似问题