匿名枚舉
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