#include <iostream>
class Base
{
public:
virtual void ok( float k ){ std::cout<< "ok..." << k; }
virtual float ok(){ std::cout<< "ok..."; return 42.0f; }
};
class Test : public Base
{
public:
void ok( float k ) { std::cout<< "OK! " << k; }
//float ok() { std::cout << "OK!"; return 42; }
};
int main()
{
Test test;
float k= test.ok();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
GCC 4.4下的汇编:
hello_world.cpp: In function `int main()`:
hello_world.cpp:28: erreur: no matching function for call to `Test::ok()`
hello_world.cpp:19: note: candidats sont: virtual void Test::ok(float)
Run Code Online (Sandbox Code Playgroud)
我不明白为什么float ok()测试用户无法访问Base中定义的内容,即使它公开继承它.我已经尝试使用指向基类的指针,它确实编译.取消注释Test的实现也是float ok()如此.
它是一个bug编译器吗?我怀疑与名字屏蔽有关的问题,但我完全不确定.
Ale*_*ler 14
它被称为名称隐藏,任何派生类字段都隐藏所有基类中所有重载的同名字段.要使方法可用Test,请添加using指令,即
using Base::ok;
Run Code Online (Sandbox Code Playgroud)
在某个范围内Test.参见更多信息这.
不,这不是一个错误.只是派生的class Test是Base::ok()由于同名而隐藏方法.只是做以下,它应该工作:
class Test : public Base
{
public:
using B::ok; // no need to declare parameters; it will allow all ok()
...
};
Run Code Online (Sandbox Code Playgroud)