Can enum class be nested?

后端 未结 4 2027
[愿得一人]
[愿得一人] 2021-02-14 17:32

Can this be done?

enum A
{
    enum B
    {
        SOMETHING1,
        SOMETHING2
    };

    enum C
    {
        SOMETHING3,
        SOMETHING4
    };
};
         


        
4条回答
  •  眼角桃花
    2021-02-14 18:25

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

提交回复
热议问题