Can enum class be nested?

后端 未结 4 1398
执笔经年
执笔经年 2021-02-14 17:33

Can this be done?

enum A
{
    enum B
    {
        SOMETHING1,
        SOMETHING2
    };

    enum C
    {
        SOMETHING3,
        SOMETHING4
    };
};
         


        
4条回答
  •  野的像风
    2021-02-14 18:22

    No, they cannot be nested that way. In fact, any compiler would reject it.

    If not is there an alternative solution?

    That mostly depends on what you are trying to achieve (solution to what problem?). If your goal is to be able to write something like A::B::SOMETHING1, you could just define them within a namespace, this way:

    namespace A
    {
        enum B
        {
            SOMETHING1,
            SOMETHING2
        };
    
        enum C
        {
            SOMETHING3,
            SOMETHING4
        };     
    }
    

提交回复
热议问题