简体   繁体   中英

initializing pointer to protected member functions

I have this problem:

In the header file:

class D : public B
{
//...
private:
    typedef char* (B::*psbposfun_t)() const;

    static psbposfun_t ms_aposf[2][3];
//...
};

In the source file:

D::psbposfun_t D::ms_aposf[2][3] = 
{
    {
        &B::fa1,
        &B::fa2,
        &B::fa3
    },
    {
        &B::fb1,
        &B::fb2,
        &B::fb3
    }
};

The compiler complains that the fa1 ... fb3 methods are protected. Indeed they are protected in B but I am initializing a member that belongs to D, which derives from B.

I tried initializing ms_aposf within the class (D) but the compiler complains it is not the place to initialize it.

So besides implementing a one-shot initialization in the constructor, would someone know how to circumvent this problem?

Use D:: instead:

D::psbposfun_t D::ms_aposf[2][3] = 
{
    {
        &D::fa1,
        &D::fa2,
        &D::fa3
    },
    {
        &D::fb1,
        &D::fb2,
        &D::fb3
    }
};

Since D is inheriting these functions, they are accessible through D , but usable as B:: pointers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM