Scoped Enumerations
In this lesson, we'll discuss how enumerations are used to define custom data-type containing integer constants.
We'll cover the following
Enumerations allow us to define a custom data-types containing integer constants. These integer constants are called enumerators. Sadly, classical enums have a few drawbacks.
Drawbacks of enumerations in classical C++ #
Three of the biggest drawbacks of classical enumerations are listed below:
-
Enumerators implicitly convert to
int
. -
Enumerators in the enclosing scope.
-
The type of the enumeration cannot be specified.
First, let’s focus on 3. Enumerations cannot be forward declared because their type is unknown. There is only one guarantee for enumerations in classical C++: the type must be integral and big enough to hold the enumerator values.
Points 1 and 2 are more surprising. Have a look at the code below:
Get hands-on with 1400+ tech skills courses.