CHAPTER 18

image

Enum

Enum is a user-defined type consisting of a fixed list of named constants. In the example below, the enumeration type is called Color and contains three constants: Red, Green and Blue.

enum Color { Red, Green, Blue };

The Color type can be used to create variables that may hold one of these constant values.

int main()
{
    Color c = Red;
}

Enum constants may be prefixed with the enum name for added clarity. However, these constants are always unscoped, and so care must be taken to avoid naming conflicts.

Color c = Color::Red;

Enum Example

The switch statement provides a good example of when enumerations can be useful. Compared to using ordinary constants, the enumeration has the advantage that it allows the programmer to clearly specify what values a variable should contain.

switch(c)
{
    case Red:   break;
    case Green: break;
    case Blue:  break;
}

Enum Constant Values

Usually there is no need to know the underlying values that the constants represent, but in some cases it can be useful. By default, the first constant in the enum list has the value zero and each successive constant is one value higher.

enum Color
{
    Red   // 0
    Green // 1
    Blue  // 2
};

These default values can be overridden by assigning values to the constants. The values can be computed and do not have to be unique.

enum Color
{
    Red   = 5,        // 5
    Green = Red,      // 5
    Blue  = Green + 2 // 7
};

Enum Conversions

The compiler can implicitly convert an enumeration constant to an integer. However, converting an integer back into an enum variable requires an explicit cast, since this conversion makes it possible to assign a value that is not included in the enum’s list of constants.

int i = Red;
Color c = (Color)i;

Enum Scope

An enum does not have to be declared globally. It can also be placed within a class as a class member, or locally within a function.

class MyClass
{
    enum Color { Red, Green, Blue };
};

void myFunction()
{
    enum Color { Red, Green, Blue };
}

Strongly Typed Enums

The enum class was introduced in C++11 to provide a safer alternative to the regular enum. These new enums are defined in the same way as regular enums, with the addition of the class keyword.

enum class Speed
{
    Fast,
    Normal,
    Slow
};

With the new enum the specified constants belong within the scope of the enum class name, as opposed to the outer scope as for regular enums. To access an enum class constant, it must therefore be qualified with the enum name.

Speed s = Speed::Fast;

The underlying integral type of the regular enum is not defined by the standard and may vary between implementations. In contrast, a class enum always uses the int type by default. This type can be overridden to another integer type, as seen below.

enum class MyEnum : unsigned short {};

One last important advantage of enum classes is their type safety. Unlike regular enums, enum classes are strongly typed and will therefore not convert implicitly to integer types.

if (s == Speed::Fast) {} // ok
if (s == 0) {}           // error
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.206.25