enum Class in C++



An enumeration, or enum in C++, is a user-defined data type formed by a set of named integral constants. However, enum types from tradition have the disadvantage of potential name clashes and the absence of type safety.

To solve this problem, C++ introduced the concept of enum class, which is also referred to as scoped enumerations.

Declaring an enum Class

This is the following syntax of declaring an enum class, which is done by using scoped enumeration (enum class).

 enum class EnumName { Value1, Value2, Value3, // ... other enumerator values. }; 

Here, EnumName is the name of the enumeration class.

Value1, Value2.. is the named value(constants) inside the enum class.

Accessing Values from an enum Class

You can access the values of an enum class by using the Scope Resolution Operator (::) with the enum class name. Consider the following syntax:

EnumName::Value

Here, EnumName is the name of the enum class. Value is one of the enumerators defined in the class.

Example of Enum Class

Here is the following example for the enum class in C++.

 #include <iostream> using namespace std; // Declare an enum class for days of the week enum class Day { Monday, // 0 Tuesday, // 1 Wednesday, // 2 Thursday, // 3 Friday, // 4 Saturday, // 5 Sunday // 6 }; int main() { Day today = Day::Friday; if (today == Day::Friday) { cout << "Today is Friday!" << endl; } return 0; } 

Output

Today is Friday!

Explanation

  • First, we declared an enum class named Day with their values.
  • And then set the variable today to Day::Friday, and then we compared it to Day::Friday to print a message.

Underlying Type of an Enum Class

The enum class uses an integer as its default underlying type but many more underlying types are possible: unsigned int, char, uint8_t, int16_t, etc.

Default Underlying Type of enum class

When you declare the enum class without specifying any underlying type, then by default it is set to int. This means that each enumerator's value will be stored as an int (4 bytes).

As we already see in the above following example.

Specifying a Custom Underlying Type for an enum class

Here, you can explicitly specify the underlying type of an enum class by adding a colon (:) after the enum class name. This will allow for control of the storage size and range of values.

Syntax

Here is the following syntax for it.

 enum class EnumName : UnderlyingType { Value1, Value2, Value3 }; 

Here, UnderlyingType could be any integral type (like unsigned int, char, uint8_t, etc.).

Example of Underlying Type of an Enum Class

Here is the following example using unsigned int as Underlying Type.

 #include <iostream> using namespace std; enum class Status : unsigned int { Ok = 0, // 0 Error = 1, // 1 Warning = 2 // 2 }; int main() { Status s = Status::Error; // Print the integer value of the enum cout << "The integer value of Error is: " << static_cast<unsigned int>(s) << endl; return 0; } 

Output

The integer value of Error is: 1
Advertisements
close