Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) enum class

 

enum class is a two-word keyword that has different meanings, depending on the standard used:

 

 

 

 

 

C++98 enum class in the C++98 standard

 

enum class is not supported in C++98.

 

 

 

 

 

C++0x enum class in the C++0x standard

 

 

An enum class is a conversion-safe enum:

 

enum class Color { red  , orange };
enum class Fruit { apple, orange };

int main()
{
  Color c = Color::orange;
  Fruit a = Fruit::orange;
  Fruit b = Color::orange; //Fails
}

 

Screen output:

 

cannot convert 'Color' to 'Fruit' in initialization

 

Technical note: the code shown is compiled successfully using the G++ 4.4.5 compiler, which is supplied with the Qt Creator 2.0.0 IDE.

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict