c++ 匿名枚举

来源:互联网 发布:北京奥运会门票知乎 编辑:程序博客网 时间:2024/05/29 14:36
匿名枚举

enum { value = 0 , value2 = 1};
enum { value3 = 2};

刚看到时候有些奇怪,经过考察,他的功能等价于静态常成员变量, 
struct IsCustomUnsignedInt

{
   
    enum { value = 0 , value2 = 1};

    enum { value3 = 2};

  
    static const int value4 = 3;
 
    static int value5;

}; 


int IsCustomUnsignedInt::value5 = 4;
void main()

{
    

 
    int result = IsCustomUnsignedInt::value;
    int result2 = IsCustomUnsignedInt::value2;

    IsCustomUnsignedInt::value3 = 3; //error C2440: '=' : cannot convert from 'int' to ''

    
//IsCustomUnsignedInt::value4 = 4; //error C3892: 'value4' : you cannot assign to a variable that is const
    IsCustomUnsignedInt::value5 = 5;
    

}

写了一个更一般的例子,果然。
 enum Type{First,Second,Third};

 struct TestEnum
{
    
   enum Type2{One,Two,Three};
 
   void Test()

    {
        
         int i = One;

    }
}
;

void main()

{

    int i = First;
    TestEnum test;
    
    test.Test();
    
    //int x = One; //error    
     int j = TestEnum::One;

}

如果有不对请指出,共同学习进步哦!thx
原创粉丝点击