类请看下面的代码:成员函数隐藏在派生
#include <iostream>
using namespace std;
class A {
public:
A() {};
virtual void foo(double d) { cout << d << endl; }
virtual void foo(double d, int a) = 0;
};
class B : public A {
public:
B() {};
virtual void foo(double d, int a) { cout << d << endl << a << endl; }
};
int main()
{
B b;
b.foo(3.14);
return 0;
}
编译器(试过G ++和Visual C++ 2008)说,有没有像B无功能:FOO(双)。 G ++的确切消息是:
main.cpp:21: error: no matching function for call to ‘B::foo(double)’
它看起来像hiding rule的效果,但在我看来,规则不应该用在这里,因为我不重写富(双)和两个FOO方法定义在基类中。
我知道我可以在派生类B.
你能解释一下为什么代码不编译和C的哪些规则++适用于这里与
using A::foo;
声明解决这一问题?
这只是“名称隐藏”问题。为什么你不能只使用'使用A :: foo'子句? – ereOn 2010-07-05 07:56:04
不是一个确切的重复,但在这里讨论相同的问题:http://stackoverflow.com/questions/72010/c-overload-resolution – ereOn 2010-07-05 08:00:12
在这种情况下,如果你想实现你的纯虚拟富方法你必须重新定义所有的方法名称“foo”或为纯虚拟方法和实施方法选择不同的名称。 – Sergey 2010-07-05 08:56:36