C++11 strongly typed enumerations

Source: Internet
Author: User

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

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.