Enumerations


An enumeration is a collection of named integer constants. The general form of an enumeration is shown below.

enum name { enumeration-list }

enumeration_list:
    enumerator
    enumeration_list enumerator
    enumeration_list, enumerator
 
enumerator:
    identifier
    identifier = constant_expression

The name of the enumeration is specified by name. An enumeration defines a new type as specified by this name. The enumeration list is a list of iidentifiers.

An example of an enumeration is shown below.

space sampleI
{
    enum greek
    {
        alpha
        beta
        gamma
    }

    sampleI()
    {
       variable = greek.alpha

       select variable
       {
            alpha
            {
                cout << "hello" << "\n"
            }

            beta
            {
                cout << "world" << "\n"
            }
        }
    }
}

Note that there is a special relationship between enumerations and the select statement. The enumerators occuring in cases of the select statement do not have to be qualified and can be used as shown above.

Printing Enumerators

The following program demonstrates printing enumerators.

// The enumerator name prints out for enumerators. This is unlike other integer types.

using system

space enum_print
{
    enum greek
    {
       alpha
       beta
       gamma
    }

    class enum_print
    {
       enum_print()
       {
           cout << greek.gamma << "\n"
           cout << (integer)greek.gamma << "\n"
       }
    }
}

And the output of the program is shown below.

gamma
2

So if you wish to print the integer value of an enumerator you cast it to integer - otherwise it prints out as the string enumerator name.

Integral and Floating Point Promotions

See Integral and Floating Point Promotions for examples of how enumerators can be promoted to integer or floating point numbers.