Enumeration, or enum for short, is a type whose values are user-defined named constants called enumerators.
There are two kinds of enums: the unscoped enums and scoped enums.
The unscoped enum type can be defined with:
enum MyEnum
{
myfirstvalue,
mysecondvalue,
mythirdvalue
};
To declare a variable of enumeration type MyEnum we write:
enum MyEnum //from ww w. j a v a2 s . co m { myfirstvalue, mysecondvalue, mythirdvalue }; int main() { MyEnum myenum = myfirstvalue; myenum = mysecondvalue; // we can change the value of our enum object }
Each enumerator has a value of underlying type. We can change those:
enum MyEnum
{
myfirstvalue = 10,
mysecondvalue,
mythirdvalue
};
These unscoped enums have their enumerators leak into an outside scope, the scope in which the enum type itself is defined.
Old enums are best avoided.
Prefer scoped enums to these old-school, unscoped enums.
Scoped enums do not leak their enumerators into an outer scope and are not implicitly convertible to other types.