c++static-methodsprivate-inheritance

How to call a static method from a private base class?


Due to the layout of a third-party library, I have something like the following code:

struct Base
{
    static void SomeStaticMethod(){}
};

struct Derived1: private Base {};

struct Derived2: public Derived1 {
    void SomeInstanceMethod(){
        Base::SomeStaticMethod();
    }
};

int main() {
    Derived2 d2;
    d2.SomeInstanceMethod();

    return 0;
}

I'm getting compiler error C2247 with MSVC:

Base::SomeStaticMethod not accessible because Derived1 uses private to inherit from Base.

I know I can't access Base members from Derived2 via inheritance because of the private specifier, but I should still be able to call a static method of Base - regardless of any inheritance relationship between Base and Derived2.
How do I resolve the ambiguity and tell the compiler I'm just making a call to a static method?


Solution

  • Do this:

    struct Derived2: public Derived1 {
        void SomeInstanceMethod(){
            ::Base::SomeStaticMethod();
    //      ^^
    //      Notice leading :: for accessing root namespace.
        }
    };