C++11 defines, the enum
class (also called a scoped enumeration), which makes enumerations both strongly typed and strongly scoped. Here’s an example:
#include <iostream>
int main()
{
enum class Color // "enum class" defines this as an scoped enumeration instead of a standard enumeration
{
RED, // RED is inside the scope of Color
BLUE
};
enum class Fruit
{
BANANA, // BANANA is inside the scope of Fruit
APPLE
};
Color color = Color::RED; // note: RED is not directly accessible any more, we have to use Color::RED
Fruit fruit = Fruit::BANANA; // note: BANANA is not directly accessible any more, we have to use Fruit::BANANA
if (color == fruit) // compile error here, as the compiler doesn't know how to compare different types Color and Fruit
std::cout << "color and fruit are equal\n";
else
std::cout << "color and fruit are not equal\n";
return 0;
}
scoped enumeration means :
enumerator names are local to the enum
and also their values do not implicitly convert to other types.
In your case you have to use enum
instead of enum calss
to prevent that kind of error which is due to the scope of the enum.