C + + Enum Strong Type Enumeration

Source: Internet
Author: User

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&LT;TYPE4::B);//A comparison of the same enum type can + if(T&GT;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

Related Article

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.