Tag:c++11 enum strongly typed enumeration
C++11 before the enum type is inherited C, tepid;//c++11 to enum dynamic knife, strengthen the type check, the introduction of strong type Enum type, before the light//used Qt know, should have done so, with very cool!! I. Limitations of enum types in C//1, non-strongly typed scopes enum type1{a, B, C};enum Type2{a, D, e};//The problem comes, both enum types have a enum constant, and the scope is the same, there is a conflict//2, allow implicit type conversion if ( Type1::a = = type2::d) {;} The comparison of two different enum constants is meaningless, but the compiler first converts the enum constant to the int type, then compares it, does not error//3, occupies the storage space extremely symbolic indeterminate enum type3{a=1, b=0xfffffffffll};cout< <sizeof (TYPE3::A) <<endl;//may be 8, depending on the compiler, it is determined by the Chi cout<<type3::b<<endl;//output size is variable, it may be 0//problem, Storage size and symbols have undetermined factors//Two, c++11 strong type enum type//syntax: enum class type4:type{a, B, c};//Plus class, named, specify underlying type Typeenum class Type5{a, B, c};// Do not specify the underlying type//features and Usage://1, strong scope type4 t = type4::a; Strong type name must be specified type4 p = A; Error//2, non-implicit conversion if (T<TYPE4::B); The same enum type can be compared to if (T>TYPE5::B); Not the same enum type, not implicitly converted to int equivalent, no, uh, the comparison if ((int) T > (int) type5::b)//coercion type conversion, can be specified by//3, the underlying type enum class type6:char{a=1, b =2};enum class type7:unsigned int{a=0xffffffff};//can save space, but also can specify the symbol//third, C++11 is compatible and extends the C enum type enum type8:char{a=1, b=2}; TypE8 T =a;//extends and is compatible with C enum types
C++11: Strongly-typed enumeration (enum)