匿名枚举
刚看到时候有些奇怪,经过考察,他的功能等价于静态常成员变量,
写了一个更一般的例子,果然。
如果有不对请指出,共同学习进步哦!thx
enum
{ value = 0 , value2 = 1};
enum { value3 = 2};
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 { 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;
}
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