Go back to Richel Bilderbeek's homepage.
Go back to Richel Bilderbeek's C++ page.
enum is a keyword to define an enumeration. Enumerations can be used to make your code better readable.
Avoid enumerations at file scope in header files [5]. Use a consistent method to identify immutable values such as enum values [6].

Example: with and without enum
Below is an example of a code that did not use enum.
//Without using enum, not preferred |
How can one expect to memorize all these values for sexes? enum relieves things:
enum Sex { male, female, hermaphrodite }; |
Of course, the example without enum can be converted to the example below, using global constants. Prefer not to use globals [1-4].
//Without using enum, not preferred |

Example how to define an enum and overloading operator<< for it
#include <iostream> |
Additional C++0x enum possibilities
C++0x supports a type-safe variant of enum, which is called an enum class.
Go back to Richel Bilderbeek's C++ page.
Go back to Richel Bilderbeek's homepage.