|
|
|
|
|
|
|
enum Days {SUN = 4, MON = 18, TUE = 9, }; |
|
|
|
|
|
|
|
|
There is rarely any reason to assign specific values to enumerators. With the Days type, we are interested in the days of the week, not in the way the machine stores them internally. We do not discuss this feature any further, although you may occasionally see it in C++programs. |
|
|
|
|
|
|
|
|
Notice the style we use to capitalize enumerators. Because enumerators are, in essence, named constants, we capitalize the entire identifier. This is purely a style choice. Many C++programmers use both uppercase and lowercase letters when they invent names for the enumerators. |
|
|
|
|
|
|
|
|
Here is the syntax template for the declaration of an enumeration type. It is a simplified version; later in the chapter we expand it. |
|
|
|
|
|
|
|
|
An enumerator has the following form: |
|
|
|
|
|
|
|
|
where the optional ConstIntExpression is an integer expression composed only of literal or named constants. |
|
|
|
|
|
|
|
|
The identifiers used as enumerators must follow the rules for any C++ identifier. For example, |
|
|
|
|
|
|
|
|
enum Vowel {A, E, I, 0, U}; // No |
|
|
|
|
|
|
|
|
is not legal because the items are not identifiers. In the declaration |
|
|
|
|
|
|
|
|
enum Places {1st, 2nd, 3rd}; // No |
|
|
|
|
|
|
|
|
type Places is not legal because identifiers cannot begin with digits. In the declarations |
|
|
|
|
|