1 //The enum type before c++11 is inherited from C, tepid;2 //c++11 to enum, strengthen the type check, the introduction of the type of strongly-typed enum, the immediate bright3 //Have used QT all know, long ago should do so, with very cool!! 4 5 //limitations of enum types in C6 //1. Non-strongly typed scopes7 8 enumType1{a, B, C}; 9 enumType2{a, D, E}; Ten One //The problem is that both enum types have a enum constant, and the scope is the same, and there is a conflict A - //2. Allow implicit type conversions - the 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, and then compares it without an error . - + //3, occupy the storage space extremely sign indeterminate - + enumType3{a=1, b=0xFFFFFFFFFLL}; Acout<<sizeof(TYPE3::A) <<endl;//may be 8, depending on the compiler, which is determined by the Chi atcout<<type3::b<<endl;//The output size is variable, possibly 0 - - //The problem is that storage size and symbols have undetermined factors . - - //Ii. c++11 strongly-typed enum type - in //Syntax: - to enum classType4:type{a, B, C};//Add class, named, specifying the underlying type + enum classType5{a, B, C};//do not specify the underlying type - the //features and usage: * //1. Strong scope $ Panax NotoginsengType4 t = type4::a;//strongly typed name must be specified -Type4 p = A;//Error the + //2. Non-implicit conversion A the if(T<TYPE4::B);//A comparison of the same enum type can + if(T>TYPE5::B);//non-identical enum type, cannot be implicitly converted to int equivalent, no, well, the comparison - if((int) T > (int) type5::b)//coercion of type conversions can be done by $ $ //3. You can specify the underlying type - - enum classType6:Char{a=1, b=2}; the enum classtype7:unsignedint{a=0xFFFFFFFF}; - Wuyi //You can save space and specify symbols the - //Third, C++11 is compatible and extends the C enum type Wu - enumType8:Char{a=1, b=2}; AboutType8 T =A; $ - //extension and compatibility with C enum types
From:41978973?utm_source=tuicool&utm_medium=referral
C + + Enum Strong Type Enumeration