Close
Close

Enum Class in C++


Sometimes, we get some result with enum which is not desirable. For example, look at the following case.

#include <iostream>
enum Season
{
    Summer,
    Spring,
    Winter,
    Autumn
};
enum Color{
    Blue,
    Pink,
    Green
};
int main()
{
    Season s = Summer;
    Color c = Blue;
    if( s == c )
    {
    	std::cout << "Equal" << std::endl;
    }
    else
    {
    	std::cout << "Not Equal" << std::endl;
    }
    return 0;
}
Output
Equal

Here, we want to check whether Summer is equal to Blue which is obviously not true since one of these is Season and the other a color. In the above example, we got the output true because Summer and Blue got converted into integers and then those integers got compared.
The values of both Summer and Blue is 0 and thus the values of s and c became 0. So, the condition ( s == c ) became true and 'Equal' got printed on the screen. This is not the desired output because we cannot compare a season and a color. Since season and color belong to different enumerations, therefore these should not be compared.

Here comes in enum class which limits the scope of the enumerators within their enums. So, now any enumerator will be known by its enum thus limiting its scope within the enum to which it belongs. This is the reason enum class is also called scoped enumeration.

We will understand with the help of an example. But before that, let's first see how to make an enum class.

To make an enum class, we simply have to add the keyword class after the keyword enum.

enum class Season
{
    Summer,
    Spring,
    Winter,
    Autumn
};

We made our enum Season an enum class by adding the keyword class after the keyword enum. Now let's see how to access any element (enumerator) of this enum class.

Season::Summer

The :: symbol simply means belong to. Here, Summer belongs to the enum class Season and thus cannot be directly accessed anywhere.

Look at the following example to understand this.

#include <iostream>
enum class Season
{
    Summer,
    Spring,
    Winter,
    Autumn
};
enum class Color{
    Blue,
    Pink,
    Green
};
int main()
{
    Season s = Season::Summer;
    Color c = Color::Blue;
    if( s == c )
    {
    	std::cout << "Equal" << std::endl;
    }
    else
    {
    	std::cout << "Not Equal" << std::endl;
    }
    return 0;
}
Output
prog.cpp:2:6: warning: scoped enumerations are a C++11 extension [-Wc++11-extensions]
enum class Season
     ^
prog.cpp:9:6: warning: scoped enumerations are a C++11 extension [-Wc++11-extensions]
enum class Color{
     ^
prog.cpp:16:16: warning: use of enumeration in a nested name specifier is a C++11 extension [-Wc++11-extensions]
    Season s = Season::Summer;
              ^
prog.cpp:17:15: warning: use of enumeration in a nested name specifier is a C++11 extension [-Wc++11-extensions]
    Color c = Color::Blue;
              ^
prog.cpp:18:11: warning: comparison of two values with different enumeration types ('Season' and 'Color') [-Wenum-compare]
    if( s == c )
        ~ ^  ~
prog.cpp:18:11: error: invalid operands to binary expression ('Season' and 'Color')
    if( s == c )
        ~ ^  ~
5 warnings and 1 error generated.

In this example, we cannot directly access the enumerators ( i.e. we cannot directly access it by writing Summer or Blue as we did in the first example ). We have to write the enum class name followed by :: before the enumerator name while accessing it because now the enumerator will be known by its enum class.
As in the above example, we accessed the Summer enumerator as Season::Summer thus telling the compiler that we are accessing Summer which is a Season. Similarly, by writing Color::Blue, we are telling the compiler that we are accessing Blue which is a Color. When we wrote the condition ( s == c ), we are comparing a Season and a Color thus getting an error.

We can compare the enumerators which belong to the same enum class.

#include <iostream>
enum class Color{
    Blue,
    Pink,
    Green
};
int main()
{
    Color c = Color::Blue;
    if( c == Color::Blue )
    {
    	std::cout << "Your color is Blue" << std::endl;
    }
    else if( c == Color::Pink )
    {
    	std::cout << "Your color is Pink" << std::endl;
    }
    else
    {
    	std::cout << "Your color is Green" << std::endl;
    }
    return 0;
}
Output
Your color is Blue

In this example, we compared the enumerators within the enum class Color. Note that enumerators are not converted to integers in the case of enum class. By writing Color c = Color::Blue;, we are assigning the color Blue of enum class Color to the variable c. So the condition ( c == Color::Blue ) became true and "Your color is Blue" got printed. In this condition, we compared c and Color::Blue, both of which belong to the enum class Color.

To learn from simple videos, you can always look at our C++ video course on CodesDope Pro. It has over 750 practice questions and over 200 solved examples.

Ask Yours
Post Yours
Doubt? Ask question