#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;
}
Compilation under 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)
I don't understand why float ok()
defined in Base isn't accessible to Test user even if it does publicly inherit from it. I've tried using a pointer to the base class and it does compile. Uncommenting the Test implementation of float ok()
works too.
Is it a bug compiler? I'm suspecting a problem related to name masking but I'm not sure at all.
It's called name hiding, any derived class field hides all overloaded fields of the same name in all base classes. To make the method available in Test
, add a using
directive, i.e.
using Base::ok;
somewhere in the scope of Test
. See this for more information.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With