C++11: Strongly-typed enumeration (enum)

Source: Internet
Author: User

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&LT;TYPE4::B);                   The same enum type can be compared to if (T&GT;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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.