Introduction to Enumerations
The source material places the names red and green in two different enumerations.
The Misconception That red Is One Fixed Thing
Suppose you are building a small program that tracks two different things: the color of a paint sample and the state of a traffic light. Both sets of options seem to need a word called red. It is tempting to think that red is simply red, one fixed thing that means the same in every part of the program. This belief is the misconception this article addresses.
What do you think happens?
You declare one enum class named Color with red, blue, and green, and another enum class named Traffic_light with green, yellow, and red. Are Color::red and Traffic_light::red the same enumerator, or two separate enumerators that happen to share a spelling?
Reveal answer
They are two separate enumerators that share a spelling.
Each enum class keeps its own list of enumerators. The enum class name in the qualified name tells you which list the red belongs to, so Color::red and Traffic_light::red are different enumerators even though both are spelled red.
Two Lists That Both Contain red
The source material places the names red and green in two different enumerations. Color contains red, blue, and green. Traffic_light contains green, yellow, and red. The same spelling appears in both lists, and that is allowed. Enumerators such as red are in the scope of their enum class, so they can be reused in different enum classes without confusion.
Reusing the word red does not force the programmer to invent a new spelling such as colorRed or lightRed. Each enum class keeps its own list, and the qualified name tells the compiler which list you mean. The diagram below shows each enum class as a container that holds its own enumerators. Notice that red appears under both containers. The two entries are separate, and the enum class that holds each one is what distinguishes them.
Declaring an enum class
An enum class is declared with the keywords enum class, followed by a name, and then a braced list of enumerators separated by commas. The declaration below comes from the source and creates an enumeration named Color with three enumerators.
- enum class: the keywords that introduce a scoped enumeration
- Color: the name of the enum class
- { red, blue, green }: the braced list of enumerators
- red, blue, green: the enumerators, separated by commas
Reading a Qualified Name
To use an enumerator, you write the enum class name, the scope operator ::, and the enumerator name. The source uses the following line to create a variable of type Color and set it to the red enumerator.
- Color on the left: declares the variable type
- col: the variable name
- Color::red on the right: names the specific enumerator
The name Color on the left declares the variable type, and Color::red on the right names the specific enumerator. The qualifier before :: is the part of the name that picks the enum class, and the part after :: picks the enumerator inside it. The bare word red by itself does not say which list it belongs to, so the qualifier is what makes the reference precise.
Tracing a Program with Two Enum Classes
The source gives both enum classes side by side. Color contains red, blue, and green. Traffic_light contains green, yellow, and red. The program below combines them. It is a short program written for this article to show the central rule: whenever you read an enumerator, the qualified name tells you which one is meant.
- 1The first declaration creates the Color enum class with red, blue, and green inside it.
- 2The second declaration creates the Traffic_light enum class with green, yellow, and red inside it. The spelling red appears again, but it belongs to a different list.
- 3The variable paint is declared as type Color and receives Color::red, so it refers to the red inside Color.
- 4The variable signal is declared as type Traffic_light and receives Traffic_light::red, so it refers to the red inside Traffic_light. The two variables hold different enumerators, even though both are written as red.
- Line 1 declares the Color list, which holds red
- Line 2 declares the Traffic_light list, which also holds red
- Line 3 sets paint to the red enumerator of Color
- Line 4 sets signal to the red enumerator of Traffic_light
Tracing which red a variable holds
Given the program above, which enumerator does signal hold, and how do you know?
- 1Find the declaration
The variable signal is declared with the type Traffic_light.
- 2Read the qualifier
The right-hand side is Traffic_light::red. The qualifier before :: is Traffic_light.
- 3Locate the list
Traffic_light was declared with green, yellow, and red, so the red named here is the one in that list.
- 4Ignore the bare spelling
Do not stop at the word red. The qualified name is what identifies the enumerator.
signal holds the red enumerator of Traffic_light. It is a different enumerator from the red enumerator of Color that paint holds.
Mistakes When Reading Enumerator Names
Common mistake Treating red as one fixed value that means the same everywhere.
What it looks likeAssuming Color::red and Traffic_light::red are the same enumerator because both are spelled red.
Why it failsEach enum class keeps its own list of enumerators, so the same spelling can appear in more than one list as a separate enumerator.
Do this insteadRead the qualified name and identify which enum class it belongs to before deciding what the value is.
Common mistake Inventing new spellings to avoid a clash.
What it looks likeRenaming the enumerators to colorRed or lightRed to make them unique.
Why it failsEnumerators in an enum class are in the scope of that class, so reuse across enum classes does not cause confusion and the renaming is unnecessary.
Do this insteadKeep the natural name and rely on the enum class qualifier to distinguish them.
Common mistake Dropping the qualifier when reading an enumerator.
Why it failsThe bare word red does not say which enum class it belongs to, so the reader cannot tell which list is meant.
Do this insteadKeep the enum class name and the :: in front of the enumerator name, as in Color::red.
Practice Tracing Qualified Names
Using the Color and Traffic_light declarations from this article, write a line that declares a variable named light of type Traffic_light and sets it to the green enumerator of Traffic_light. Then state whether light and a variable declared as Color paint = Color::green would hold the same enumerator, and explain why.
Hints
- Use the pattern Type name = Type::enumerator; from the qualified name section.
- Compare the qualifiers: Traffic_light::green and Color::green differ in the enum class before ::.
- Both values are spelled green, but each belongs to a different enum class.
Key Takeaways
- An enum class declares a named set of enumerators inside braces, such as enum class Color { red, blue, green };
- Enumerators are in the scope of their enum class, so the same name, such as red, can appear in different enum classes without confusion.
- Color::red and Traffic_light::red are different enumerators even though both are spelled red.
- When reading or tracing code, the qualified name, including the enum class name and ::, identifies which enumerator is meant.