Enumeration is to define a category and enumerate the individuals under the unified category for code use.
Defects of C + + 98 enumeration:
-
Whether namedEnumeration namestillEnumerating members in types, bothGlobal scopeYes, itsThe scope is globalof Appears if the same enumeration member is defined in a different enumerationRedeclarationWrong.
enum PUBLIC_COLOR { RED, YELLOW, GREEN }; enum PRIVATE_ COLOR { RED, BLACK, PURPLE }; // Red redefinition enum PUBLIC_ COLOR { WHITE, GRAY }; // PUBLIC_ Color redefinition int main() { int color = RED; cout << color << endl; }
You may think of using namespaces to encapsulate enumeration types, but if a namespace misses a name, it will become an anonymous space, and all enumeration members in the anonymous space will become members under the global namespace by default. Therefore, this method is not appropriate.
-
The members in the enumeration are designed as aliases of constant numbers, so they can be implicitly converted to
int
Type, but this is not expected at some times. -
The space occupied by the enumeration type lock and the symbol are uncertain.
Strong enumeration type syntax format in C + + 11
enum class enum_name: elem_type { elem1, elem2, ... };
Advantages of strong enumeration type in C + + 11
-
Strong scope, the name of a strongly typed enumeration member is not output to its parent scope space.
enum class PUBLIC_COLOR { RED, GREEN, BLACK }; // ok enum PRIVATE_COLOR { RED, PURPLE, PINK = 1 }; // ok
-
Conversion restrictions, the values of strongly typed enumeration members cannot be implicitly converted to and from each other.
enum class PUBLIC_COLOR { RED, GREEN, BLACK }; enum PRIVATE_COLOR { RED, PURPLE, PINK = 1 }; If (public_color:: Green = = 1) {// errror: no acceptable type conversion defined cout << "color1 == 1" << endl; } if (PRIVATE_COLOR::PURPLE == 1) { // ok cout << "color2 == 1" << endl; }
-
You can specify the underlying type. The default underlying type of strongly typed enumeration is
int
The underlying type can also be specified explicitly.enum class PUBLIC_COLOR: char { RED, GREEN, BLACK }; enum PRIVATE_COLOR: int { RED, PURPLE, PINK }; cout << sizeof(PUBLIC_COLOR::GREEN) << endl; // 1 cout << sizeof(PRIVATE_COLOR::PURPLE) << endl; // 4
-
It is not possible to compare enumeration values of different enumeration types. However, if the specified values are the same between the same enumeration values, they can be compared.
enum class PUBLIC_COLOR { RED, GREEN, BLACK }; enum PRIVATE_COLOR { RED, PURPLE, PINK = 1 }; if (PUBLIC_COLOR::GREEN == PRIVATE_COLOR::PURPLE) { // errror cout << "PUBLIC_COLOR::GREEN == PRIVATE_COLOR::PURPLE" << endl; } if (PRIVATE_COLOR::PURPLE == PRIVATE_COLOR::PINK) { // ok cout << "PRIVATE_COLOR::PURPLE == PRIVATE_COLOR::PINK" << endl; }
-
If you want to output enumerated values, you can cast them
enum class PUBLIC_COLOR: char { RED, GREEN, BLACK }; enum PRIVATE_COLOR: int { RED, PURPLE, PINK }; cout << static_cast(PUBLIC_COLOR::RED) << endl; // 0 cout << static_cast(PRIVATE_COLOR::PURPLE) << endl; // 1