The problem: representing series of values
It is very common to have a series of values that need to be represented. For example, to simulate a traffic light requires representing three values (red, yellow, and green), but there is no built-in C++ color datatype.
Use integer values to represent colors, for example red as 0, yellow as 1, and green as 2. There is nothing “green” about the value 2, and it could just as easily be represented by some other number. However, it is common to start a series at zero and continue up by ones.
The danger of magic numbers
Use of these “magic” numbers in the source code makes the code unreadable. For example,
x = 1;
```What does this do, assign the number one or the color yellow to x?
Use of numbers is also very error prone - it is easy to mistakenly use the wrong one and making changes to the numbers and making updates to all references is difficult.
Use names instead of numbers
----------------------------
A better solution is to create named constants for each of the values. By convention, these named constants are uppercase.
const int RED = 0;
const int YELLOW = 1;
const int GREEN = 2;
int y;
int x;
. . .
y = 1; // assigns the integer one
x = YELLOW; // assigns yellow (which happens to be 1).
The enum type declaration provides a solution
---------------------------------------------
C++ uses the `enum` statement to assign sequential integer values to names _and_ provide a type name for declaration.
enum TrafficLightColor {RED, YELLOW, GREEN};
. . .
int y;
TrafficLightColor x;
. . .
y = 1;
x = YELLOW;
Type checking prevents some erroneous assignments
-------------------------------------------------
The compiler _may_ issue an error message or warning if you try to assign one kind of enum to a different kind. It also allows some dangerous types of assignments.
enum TrafficLightColor {RED, YELLOW, GREEN};
enum Gender {MALE, FEMALE};
TrafficLightColor x;
int i;
. . .
x = YELLOW; // good
i = x; // Legal, but bad style. Assigns the integer representation.
i = (int)x; // As above, explicit casting is better style.
x = (TrafficLightColor)2; // Legal, but very dangerous. No checking.
x = FEMALE; // BAD, Compiler may give error or warning.
x = 5; // BAD, Compiler may give error or warning.
Setting enum values
-------------------
It's possible to control the values that are assigned to each enum constant. If a value is assingned to a constant, each successive constant without a value is assigned a value one greater than the previous. enum Day {MON=1, TUE, WED, THU, FRI, SAT, SUN}; The value of MON is one, TUE is two, etc instead of starting at zero. Another use of specific values is to create _sets_. Explicitly setting the values to powers of two represents each as separate bit. These values can then manipulated using the bit operations (&, |, ^ and ~).
enum Day {MON=1, TUE=2, WED=4, THU=8, FRI=16, SAT=32, SUN=64}; const int WEEKDAY = MON+TUE+WED+THU+FRI; . . . Day today; // This will have one of the values in it. . . . if ((today & WEEKDAY) != 0) . . .
Enum I/O
--------
I/O of enums uses their integer values, not their names. This is not what is desired normally, so extra programming is required on input and output to use the names instead of integer values. The extra work for enum I/O means that they are often not used for simple programs.
Other languages
---------------
**Java** will have type-safe enums in version 1.5. Currently it requires programmers to explicitly declare each name as a constant ints. **C#** provides enums with additional facilities, eg to get names and check values.
The enum keyword is used to create an enumerated type named name that consists of the elements in name-list. The var-list argument is optional, and can be used to create instances of the type along with the declaration. For example, the following code creates an enumerated type for colors:
enum ColorT {red, orange, yellow, green, blue, indigo, violet};
…
ColorT c1 = indigo;
if( c1 == indigo ) {
cout « “c1 is indigo” « endl;
}
enum ColorT { red = 10, blue = 15, green };
…
ColorT c = green;
cout « “c is " « c « endl;
c is 16
enum ColorT { red = 10, blue = 15, green };
…
enum ColorT c = green; /* note the additional enum keyword */
printf( “c is %d\n”, c );
typedef enum ColorT { red = 10, blue = 15, green } ColorT;
…
ColorT c = green; /* no more additional enum keyword */
printf( “c is %d\n”, c );