c – 如何从私有基类调用静态方法?

由于第三方库的布局,我有一些像下面的代码:

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;
}

我收到编译器错误C2247与MSVC:

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

我知道我不能访问Base成员从Derived2通过继承,因为私有说明符,但我仍然应该能够调用一个静态方法Base – 无论Base和Derived2之间的任何继承关系。
如何解决歧义,告诉编译器我只是调用静态方法?

做这个:

struct Derived2: public Derived1 {
    void SomeInstanceMethod(){
        ::Base::SomeStaticMethod();
//      ^^
//      Notice leading :: for accessing root namespace.
    }
};
http://stackoverflow.com/questions/39349998/how-to-call-a-static-method-from-a-private-base-class

本站文章除注明转载外,均为本站原创或编译
转载请明显位置注明出处:c – 如何从私有基类调用静态方法?