C Primer Plus (5th Edition)
2.7. Enumerations
Often we need to define a set of alternative values for some attribute. A file, for example, might be open in one of three states: input, output, and append. One way to keep track of these state values might be to associate a unique constant number with each. We might write the following: const int input = 0; const int output = 1; const int append = 2; Although this approach works, it has a significant weakness: There is no indication that these values are related in any way. Enumerations provide an alternative method of not only defining but also grouping sets of integral constants. Defining and Initializing Enumerations
An enumeration is defined using the enum keyword, followed by an optional enumeration name, and a comma-separated list of enumerators enclosed in braces. // input is 0, output is 1, and append is 2 enum open_modes {input, output, append};
By default, the first enumerator is assigned the value zero. Each subsequent enumerator is assigned a value one greater than the value of the enumerator that immediately precedes it. Enumerators Are const Values
We may supply an initial value for one or more enumerators. The value used to initialize an enumerator must be a constant expression. A constant expression is an expression of integral type that the compiler can evaluate at compile time. An integral literal constant is a constant expression, as is a const object (Section 2.4, p. 56) that is itself initialized from a constant expression. For example, we might define the following enumeration: // shape is 1, sphere is 2, cylinder is 3, polygon is 4 enum Forms {shape = 1, sphere, cylinder, polygon}; In the enum Forms we explicitly assigned shape the value 1. The other enumerators are implicitly initialized: sphere is initialized to 2, cylinder to 3, and polygon to 4. An enumerator value need not be unique. // point2d is 2, point2w is 3, point3d is 3, point3w is 4 enum Points { point2d = 2, point2w, point3d = 3, point3w };
In this example, the enumerator point2d is explicitly initialized to 2. The next enumerator, point2w, is initialized by default, meaning that its value is one more than the value of the previous enumerator. Thus, point2w is initialized to 3. The enumerator point3d is explicitly initialized to 3, and point3w, again is initialized by default, in this case to 4. It is not possible to change the value of an enumerator. As a consequence an enumerator is itself a constant expression and so can be used where a constant expression is required. Each enum Defines a Unique Type
Each enum defines a new type. As with any type, we can define and initialize objects of type Points and can use those objects in various ways. An object of enumeration type may be initialized or assigned only by one of its enumerators or by another object of the same enumeration type: Points pt3d = point3d; // ok: point3d is a Points enumerator Points pt2w = 3; // error: pt2w initialized with int pt2w = polygon; // error: polygon is not a Points enumerator pt2w = pt3d; // ok: both are objects of Points enum type
Note that it is illegal to assign the value 3 to a Points object even though 3 is a value associated with one of the Points enumerators. |