is it possible to hide an overloaded method while using private inheritence in c++

106 Views Asked by At
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"

2

There are 2 best solutions below

0
On BEST ANSWER

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....

1
On

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.