Concepts / Introduction to Enumerations

Introduction to Enumerations

The source material places the names red and green in two different enumerations.

  • Programming

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?

  • They are the same enumerator, because the spelling red is identical

  • They are two separate enumerators that share a spelling

Reveal answer
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.

Colorenum classredColor's redTraffic_lightenum classblueColor's bluegreenColor's greengreenTraffic_light's greenyellowTraffic_light's yellowredTraffic_light's redcontainscontainscontainscontainscontainscontains
Where does the scope of Color end and the scope of Traffic_light begin, so that each red belongs to exactly one enum class? Each enum class is a container, and its enumerators sit inside that boundary.

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.

cpp
Notes on the code
  • 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
enum classintroduces a scopedenumerationColorname of the enum class{opens the enumerator listred, blue, greenthe enumerators, commaseparated};closes the declaration
What are the parts of enum class Color { red, blue, green }; and what does each part contribute?

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.

cpp
Notes on the code
  • 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.

Color::redColor's red enumeratorTraffic_light::redTraffic_light's redenumeratorColor::greenColor's green enumeratorTraffic_light::greenTraffic_light's greenenumerator
Given the qualifier and the enumerator name, which specific enumerator does each qualified name refer to?

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.

cpp
Walk through it
  1. 1
    The first declaration creates the Color enum class with red, blue, and green inside it.
  2. 2
    The 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.
  3. 3
    The variable paint is declared as type Color and receives Color::red, so it refers to the red inside Color.
  4. 4
    The 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.
Notes on the code
  • 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
Worked example

Tracing which red a variable holds

Given the program above, which enumerator does signal hold, and how do you know?

  1. 1
    Find the declaration

    The variable signal is declared with the type Traffic_light.

  2. 2
    Read the qualifier

    The right-hand side is Traffic_light::red. The qualifier before :: is Traffic_light.

  3. 3
    Locate the list

    Traffic_light was declared with green, yellow, and red, so the red named here is the one in that list.

  4. 4
    Ignore the bare spelling

    Do not stop at the word red. The qualified name is what identifies the enumerator.

Result

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 like

    Assuming Color::red and Traffic_light::red are the same enumerator because both are spelled red.

    Why it fails

    Each 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 instead

    Read 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 like

    Renaming the enumerators to colorRed or lightRed to make them unique.

    Why it fails

    Enumerators 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 instead

    Keep the natural name and rely on the enum class qualifier to distinguish them.

  • Common mistake

    Dropping the qualifier when reading an enumerator.

    Why it fails

    The bare word red does not say which enum class it belongs to, so the reader cannot tell which list is meant.

    Do this instead

    Keep the enum class name and the :: in front of the enumerator name, as in Color::red.

Practice Tracing Qualified Names

EASY

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.

Related concepts

More concepts starting with I