初始化私有成员静态常量数组 [英] Initializing private member static const array

查看:1143
本文介绍了初始化私有成员静态常量数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  MyClass类
{
上市:
         ...
私人的:
    枚举类BDTNodeType:unsigned char型
    {
        NT_TERMINAL_ZERO,
        NT_TERMINAL_ONE,
        NT_TERMINAL_X,
        NT_NOT_TERMINAL
    };    类BDTNode
    {
    上市:
        明确BDTNode(BDTNodeType NODE_TYPE = BDTNodeType :: NT_NOT_TERMINAL);
        BDTNode(常量BDTNode&安培;节点);
        〜BDTNode();        BDTNodeType类型;
        BDTNode * thenPtr; // 1
        BDTNode * elsePtr; // 0
    };    BDTNode * root_node;    //恒节点
    静态常量BDTNode fv_nodes [3] = {
        BDTNode(BDTNodeType :: NT_TERMINAL_ZERO)
        BDTNode(BDTNodeType :: NT_TERMINAL_ONE)
        BDTNode(BDTNodeType :: NT_TERMINAL_X)
    };
};

我想直接初始化静态常量BDTNode fv_nodes数组类声明中,因为C ++ 11允许这样做。但我得到C2864:'MyClass的:: fv_nodes:用在类的初始化必须有非易失性const的整体静态数据成员。我不能初始化课外,因为在这种情况下,BDTNode类将无法访问。所以,我应该怎么做呢?


解决方案

  

C ++ 11允许这样做。


没有,C ++ 11不允许这样做。你也许会想非静态成员initialisers,被允许作为替代在构造函数初始化来的。

有关静态成员,规则没有多大变化。内的类声明是不是一个定义,并且除非它是一个非易失性const的积分或枚举类型和不ODR使用的,它需要的类以外的单一的定义。如果你提供了一个初始化器,即那张定义。


  

和我不能初始化课外,因为在这种情况下,BDTNode类将无法访问。


没有,初始化剂是在类的范围,所以私人的名字都可以访问。以下 rel=\"nofollow\">作品:

 类//
静态常量BDTNode fv_nodes [3];//在源文件中
常量MyClass的:: BDTNode MyClass的:: fv_nodes [3] = {
    BDTNode(BDTNodeType :: NT_TERMINAL_ZERO)
    BDTNode(BDTNodeType :: NT_TERMINAL_ONE)
    BDTNode(BDTNodeType :: NT_TERMINAL_X)
};

class MyClass
{
public:
         ...
private:
    enum class BDTNodeType : unsigned char
    {
        NT_TERMINAL_ZERO,
        NT_TERMINAL_ONE,
        NT_TERMINAL_X,
        NT_NOT_TERMINAL
    };

    class BDTNode
    {
    public:
        explicit BDTNode(BDTNodeType node_type = BDTNodeType::NT_NOT_TERMINAL);
        BDTNode(const BDTNode &node);
        ~BDTNode();

        BDTNodeType type;
        BDTNode *thenPtr;   //1
        BDTNode *elsePtr;   //0
    };

    BDTNode *root_node;

    //Constant nodes
    static const BDTNode fv_nodes[3] = {
        BDTNode(BDTNodeType::NT_TERMINAL_ZERO),
        BDTNode(BDTNodeType::NT_TERMINAL_ONE),
        BDTNode(BDTNodeType::NT_TERMINAL_X)
    };
};

I want to initialize static const BDTNode fv_nodes array directly inside the class declaration, since C++11 allows to do this. But I get "C2864: 'MyClass::fv_nodes' : a static data member with an in-class initializer must have non-volatile const integral". And I can't initialize it outside class, because in that case "BDTNode" class would be inaccessible. So how should I do this?

C++11 allows to do this

No, C++11 doesn't allow this. You may be thinking of non-static member initialisers, which are allowed as an alternative to initialisation in a constructor.

For static members, the rules haven't changed much. The in-class declaration is not a definition and, unless it's a non-volatile const integral or enumeration type and not odr-used, it needs a single definition outside the class. If you provide an initialiser, that goes on the definition.

And I can't initialize it outside class, because in that case "BDTNode" class would be inaccessible.

No, the initialiser is in the scope of the class, so private names are accessible. The following works for me:

// in class
static const BDTNode fv_nodes[3];

// in a source file
const MyClass::BDTNode MyClass::fv_nodes[3] = {
    BDTNode(BDTNodeType::NT_TERMINAL_ZERO),
    BDTNode(BDTNodeType::NT_TERMINAL_ONE),
    BDTNode(BDTNodeType::NT_TERMINAL_X)
};

这篇关于初始化私有成员静态常量数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆