C++ allows programmers to create their own data types. Perhaps the simplest method for doing so is via an enumerated type. An
enumerated type is a data type where every possible value is defined as a symbolic constant (called an
enumerator). Enumerated types are declared via the
enum keyword. Let’s look at an example:
17 | Color eColor = COLOR_WHITE; |
Defining an enumerated type does not allocate any memory. When a variable of the enumerated type is declared (such as eColor in the example above), memory is allocated for that variable at that time.
Enum variables are the same size as an int variable. This is because each enumerator is automatically assigned an integer value based on it’s position in the enumeration list. By default, the first enumerator is assigned the integer value 0, and each subsequent enumerator has a value one greater than the previous enumerator:
13 | Color eColor = COLOR_WHITE; |
The cout statement above prints the value 4.
It is possible to explicitly define the value of enumerator. These integer values can be positive or negative and can be non-unique. Any non-defined enumerators are given a value one greater than the previous enumerator.
Because enumerated values evaluate to integers, they can be assigned to integer variables:
1 | int nValue = ANIMAL_PIG; |
However, the compiler will not implicitly cast an integer to an enumerated value. The following will produce a compiler error:
It is possible to use a static_cast to force the compiler to put an integer value into an enumerated type, though it’s generally bad style to do so:
1 | Animal eAnimal = static_cast<Animal>(5); |
Each enumerated type is considered a distinct type. Consequently, trying to assign enumerators from one enum type to another enum type will cause a compile error:
1 | Animal eAnimal = COLOR_BLUE; |
Enumerated types are incredibly useful for code documentation and readability purposes when you need to represent a specific number of states.
For example, functions often return integers to the caller to represent error codes when something went wrong inside the function. Typically, small negative numbers are used to represent different possible error codes. For example:
However, using magic numbers like this isn’t very descriptive. An alternative method would be through use of an enumerated type:
04 | ERROR_OPENING_FILE = -1, |
05 | ERROR_READING_FILE = -2, |
06 | ERROR_PARSING_FILE = -3, |
09 | ParseResult ParseFile() |
12 | return ERROR_OPENING_FILE; |
14 | return ERROR_READING_FILE; |
16 | return ERROR_PARSING_FILE; |
This is much easier to read and understand than using magic number return values. Furthermore, the caller can test the function’s return value against the appropriate enumerator, which is easier to understand than testing the return result for a specific integer value.
1 | if (ParseFile() == SUCCESS) |
Another use for enums is as array indices, because enumerator indices are more descriptive than integer indices. We will cover this in more detail in the section on arrays.
Finally, as with constant variables, enumerated types show up in the debugger, making them more useful than #defined values in this regard.
Quiz
1) Define an enumerated type to choose between the following monster types: orcs, goblins, trolls, ogres, and skeletons.
2) Declare a variable of the enumerated type you defined in question 1 and assign it the troll type.
3) True or false. Enumerators can be:
3a) explicitly assigned integer values
3b) not explicitly assigned a value
3c) explicitly assigned floating point values
3d) negative
3e) non-unique
3f) assigned the value of prior enumerators (eg. COLOR_MAGENTA = COLOR_RED)
No comments:
Post a Comment