Mastering C++: Part 3: Enumerators

Learning C++

Using C++

Mastering C++


Starting with C, there exists a way to define a fixed set of data. These are enumerators. Used through the keyword enum, this type of programming feature is often used to define certain values that will not be changed.

Enumerators work like arrays, in that their values are set to their order. In the example of the days of the week, the entry Sunday would have the value 0.

Associated Values

As a set of entries, enumerators can also have associated values. It is possible to use enumerators to hold values that stand for others.

Using Enumerators

Much like using objects in C++, an enumerator is a blueprint for entries. To use an enumerator, it must be defined in regard to a variable name.

When used as an associated value, a variable will have the value associated with the name in the enumerator. Otherwise, it will have the value of its index in the set.

Play with the example on!