1. Enumeration type
Organizes a set of integer constants together.
1.1 scoped enumeration
- Keyword enum class (enum struct)
- Enumeration member list (enumerator)
eg.
enum class open_modes { input, output, append };
1.2 unbounded enumeration type
Omit the keyword class
eg. enum color { red, yellow, green }; // Unnamed, scopeless enumeration type enum { floatPrec = 6, doublePrec = 10, double_doublePrec = 10 };
1.3 enumerating members
In a scoped enumeration type, the name of the enumeration member follows the general scope guidelines and is inaccessible outside the scope of the enumeration type
enum color { red, yellow, green }; // Scopeless enumeration type enum stoplight {red, yellow, green };//Error: duplicate enumeration member defined enum class peppers { red, yellow, green }; / / Correct: shepherd lift members are hidden color eyes = green; // Correct: the enumeration member of the scopeless enumeration type is in a valid scope peppers p = green;//Error: the enumeration member of peppers is not in a valid scope, and color::green is in a valid scope, but the type is wrong color hair = color::red;/ / Correct: allows explicit access to enumeration members peppers p2 = peppers::red;/ / Correct: use pappers of red
1.4 by default, enumeration values start with o and then increase by 1. However, we can also specify special values for one or more enumeration members
enumclass mtTypes { charTyp = 8, shortTyp = 16, intTyp = 16, longTyp = 32, long longTyp = 64 }
1.5 define and initialize enum members.
- To initialize an enum object assignment, you must use an enum member of this type or another object of this type
open_modes om = 2; / / Error: 2 is not of type open_modes om = open_modes::input;/ / correct: input yes open_modes An enumeration member of
- Each enumeration member itself is a constant expression. You can use enumeration members wherever constant expressions are needed.
eg. You can define the type of enumeration constexpr Variable: constexpr intTypes charbits = intTypes::charTyp;
1.6 convert to integer
An object or enumeration member of an unbounded enumeration type is automatically converted to an integer
int i = color::red; // Correct: the enumeration members of the scopeless enumeration type are implicitly converted to int int j = peppers::red;/ / Error: scoped enumeration types will not be implicitly converted
1.7 specify the size of the enum
Although each enum defines a unique type, enum is represented by some integer type. The default is int, or you can specify it yourself.
enum int Values : unsigned long long { charTyp = 255, shortTyp = 65535, intTyp = 65535, longTyp = 4294967295UL, long_longTyp = 18446744073709551615ULL }
1.8 pre declaration of enumeration type
- The scopeless enum does not specify the default size of the member, so each declaration must specify the size of the member.
- Scoped enum s may not specify the size of their members, which is implicitly defined as int
//Pre declaration of scopeless enumeration type intValues enum intValues : unsigned long long; // Member type must be specified if the scope is not qualified enum class open_modes; / / Scoped enumeration types can use the default member type int
1.9 declarations and definitions must match
// Error: all declarations and definitions must be consistent with whether the enum is scoped or not enum class intValues; enum intValues; / / Error: intValues Has been declared scoped enum intValues : long; // Error: intValues has been declared as int
1.10 parameter matching and enumeration type
//Enumeration types that are not scoped. Potential types vary from machine to machine enum Tokens { INLINE = 128, VIRTUAL = 129 }; void ff(Tokens); void ff(int); int main ( ) { Tokens curTok = INLINE; ff (128); //Exact match ff(int) ff (INLINE);//Exact match ff(Tokens) ff (curTok);//Exact match ff(Tokens) return 0; }
Although we cannot directly pass the integer value to the enum parameter, we can pass an object or enum member of an enumeration type that does not limit the scope to the integer parameter. At this time, the value of enum is promoted to int or greater integer, and the actual promotion result is determined by the potential class type of the enumeration type:
void newf (unsigned char); void newf (int); unsigned char uc = VIRTUAL; newf(VIRTUAL);//Call newf(int) newf(uc);// Call newf(unsigned char)