1
class Foo
{
public:
    int fn()
    {
        return 1;
    }
    int fn(int i)
    {
        return i;   //2nd fn()
    }
};
class Bar:Foo
{
public :
    Foo::fn;
};
int main(int argc, char** argv)
{
    Bar b;
    cout<<b.fn(2)<<endl;
}

is to possible to hide fn(int) in the concrete class "Bar"

sehe
  • 374,641
  • 47
  • 450
  • 633
Sleiman Jneidi
  • 22,907
  • 14
  • 56
  • 77

2 Answers2

1

AFAIK no, you cannot 'hide' names from namespaces. This related to names, so that includes all possible overloads of the same name.

Similarly, there is no way to unusing a name/namespace.

This very phenomenon leads to the rather little-known ADL Pitfall that library writers should always be aware of.


PS. in the sample code, of course you could just drop the line saying /*using*/ Foo::fn; line... but I guess that is not your actual code....

Community
  • 1
  • 1
sehe
  • 374,641
  • 47
  • 450
  • 633
1

just make the base class private, protected (it's private by default when using class, so far it,s ok) and don't use, but override the function in the derived class

class Bar: private Foo
{
public:
    int fn() {return Foo::fn();}
};

This will only make only int fn() visible in Bar and not int fn(int). Of course, the compiler will cry out loud, that fn is not a virtual function, yet you still override it, but as long as it only calls the one in the base class, its all the same.

Evan Dark
  • 1,311
  • 7
  • 7
  • This doesn't override the method `int fn()`, it instead _hides_ it by declaring a new identifier in the enclosed namespace. I don't think `it is all the same`: with virtual, the class layout could be different, ; different optimizations would apply and the semantics of the class when subclassed would be different. I would agree that, in your sample, the observed behaviour (other than performance) would be the same. It is a good workaround for the OP +1 – sehe Nov 28 '11 at 23:58