Programming Tutorials

Enumerations in C++

By: Stanley B. in C++ Tutorials on 2011-03-12  

Often we need to define a set of alternative values for some attribute. A file, for example, might be open in one of three states: input, output, and append. One way to keep track of these state values might be to associate a unique constant number with each. We might write the following:

     const int input = 0;pre
     const int output = 1;pre
     const int append = 2;pre

Although this approach works, it has a significant weakness: There is no indication that these values are related in any way. Enumerations provide an alternative method of not only defining but also grouping sets of integral constants.pre pre Defining and Initializing Enumerations

An enumeration is defined using the enum keyword, followed by an optional enumeration name, and a comma-separated list of enumerators enclosed in braces.

     // input is 0, output is 1, and append is 2pre
     enum open_modes {input, output, append};pre

By default, the first enumerator is assigned the value zero. Each subsequent enumerator is assigned a value one greater than the value of the enumerator that immediately precedes it.pre pre Enumerators Are const Values

We may supply an initial value for one or more enumerators. The value used to initialize an enumerator must be a constant expression. A constant expression is an expression of integral type that the compiler can evaluate at compile time. An integral literal constant is a constant expression, as is a const object that is itself initialized from a constant expression.pre pre For example, we might define the following enumeration:

     // shape is 1, sphere is 2, cylinder is 3, polygon is 4pre
     enum Forms {shape = 1, sphere, cylinder, polygon};pre

In the enum Forms we explicitly assigned shape the value 1. The other enumerators are implicitly initialized: sphere is initialized to 2, cylinder to 3, and polygon to 4.pre pre An enumerator value need not be unique.

     // point2d is 2, point2w is 3, point3d is 3, point3w is 4pre
     enum Points { point2d = 2, point2w,pre
                   point3d = 3, point3w };pre

In this example, the enumerator point2d is explicitly initialized to 2. The next enumerator, point2w, is initialized by default, meaning that its value is one more than the value of the previous enumerator. Thus, point2w is initialized to 3. The enumerator point3d is explicitly initialized to 3, and point3w, again is initialized by default, in this case to 4.pre pre It is not possible to change the value of an enumerator. As a consequence an enumerator is itself a constant expression and so can be used where a constant expression is required.pre pre Each enum Defines a Unique Type

Each enum defines a new type. As with any type, we can define and initialize objects of type Points and can use those objects in various ways. An object of enumeration type may be initialized or assigned only by one of its enumerators or by another object of the same enumeration type:

     Points pt3d = point3d; //  ok: point3d is a Points enumeratorpre
     Points pt2w = 3;       //  error: pt2w initialized with intpre
     pt2w = polygon;        //  error: polygon is not a Points enumeratorpre
     pt2w = pt3d;           //  ok: both are objects of Points enum typepre

Note that it is illegal to assign the value 3 to a Points object even though 3 is a value associated with one of the Points enumerators.pre






Add Comment

* Required information
1000

Comments

No comments yet. Be the first!

Most Viewed Articles (in C++ )

Latest Articles (in C++)