C++11 strongly typed enumerations
#define_crt_secure_no_warnings#include<iostream>#include<string>#include<vector>#include<map>//C + + 11 introduces a new enumeration type, "Enum class", also known as "strongly typed enumeration". Declaring type enumeration is very simple, just add the use of class or struct after enum. enumOld{yes, No};//Old styleenum classNew1{yes, No};//New Styleenum structNew2{yes, No};//New Stylevoidmytest () {/** * "traditional" C + + enumeration types have some drawbacks: * It throws enum-type members in a code interval (if the two enumerated types in the same Code field have the same name as enumeration members, which causes naming conflicts), * they are implicitly converted to integers, and cannot specify enumerations The underlying data type. */ enumStatus1{ok, Error}; //enum Status2{ok, Error};//ERR, which causes a naming conflict, Status1 already has members called OK, Error//in C++11, the strongly typed enumeration resolves these issues enum classStatusN1 {Ok, Error}; enum structStatusN2 {Ok, Error}; //StatusN1 flagN1 = Ok;//err, you must use a strongly typed nameStatusN2 flagN2 =Statusn2::ok; enum classE |Char{C1 =1, C2 =2};//specifies the underlying data type of the enumeration enum classd:unsignedint{D1 =1, D2 =2, Dbig =0xfffffff0u }; Std::cout<<sizeof(C::C1) << Std::endl;//1Std::cout <<sizeof(D::D 1) << Std::endl;//4Std::cout <<sizeof(D::D big) << Std::endl;//4 return;}intMain () {mytest (); System ("Pause"); return 0;}
C++11 strongly typed enumerations