简体   繁体   English

初始化指向受保护成员函数的指针

[英]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. 编译器抱怨fa1 ... fb3方法受到保护。 Indeed they are protected in B but I am initializing a member that belongs to D, which derives from B. 实际上,它们在B中受到保护,但是我正在初始化一个属于D的成员,该成员源自B。

I tried initializing ms_aposf within the class (D) but the compiler complains it is not the place to initialize it. 我尝试在类(D)中初始化ms_aposf,但编译器抱怨这不是初始化它的地方。

So besides implementing a one-shot initialization in the constructor, would someone know how to circumvent this problem? 因此,除了在构造函数中实现一次性初始化之外,还会有人知道如何规避此问题吗?

Use D:: instead: 使用D::代替:

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. 由于D继承了这些函数,因此可以通过D来访问它们,但可用作B::指针。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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