Team LiB
Previous Section Next Section

2.7. Enumerations

2.7. 枚举

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;
     const int output = 1;
     const int append = 2;

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.

虽然这种方法也能奏效,但是它有个明显的缺点:没有指出这些值是相关联的。枚举提供了一种替代的方法,不但定义了整数常量集,而且还把它们聚集成组。

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.

枚举的定义包括关键字 enum,其后是一个可选的枚举类型名,和一个用花括号括起来、用逗号分开的枚举成员列表。

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

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.

默认地,第一个枚举成员赋值为 0,后面的每个枚举成员赋的值比前面的大 1。

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 (Section 2.4, p. 56) that is itself initialized from a constant expression.

可以为一个或多个枚举成员提供初始值,用来初始化枚举成员的值必须是一个常量表达式。常量表达式是编译器在编译时就能够计算出结果的整型表达式。整型字面值常量是常量表达式,正如一个通过常量表达式自我初始化的 const 对象(第 2.4 节)也是常量表达式一样。

For example, we might define the following enumeration:

例如,可以定义下列枚举类型:

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

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.

枚举类型 Forms 中,显式将 shape 赋值为 1。其他枚举成员隐式初始化:sphere 初始化为 2cylinder 初始化为 3polygon 初始化为 4

An enumerator value need not be unique.

枚举成员值可以是不唯一的。

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

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.

本例中,枚举成员 point2d 显式初始化为 2。下一个枚举成员 point2w 默认初始化,即它的值比前一枚举成员的值大 1。因此 point2w 初始化为 3。枚举成员 point3d 显式初始化为 3。一样,point3w 默认初始化,结果为 4

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.

不能改变枚举成员的值。枚举成员本身就是一个常量表达式,所以也可用于需要常量表达式的任何地方。

Each enum Defines a Unique Type

每个 enum 都定义一种唯一的类型

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:

每个 enum 都定义了一种新的类型。和其他类型一样,可以定义和初始化 Points 类型的对象,也可以以不同的方式使用这些对象。枚举类型的对象的初始化或赋值,只能通过其枚举成员或同一枚举类型的其他对象来进行:

     Points pt3d = point3d; //  ok: point3d is a Points enumerator
     Points pt2w = 3;       //  error: pt2w initialized with int
     pt2w = polygon;        //  error: polygon is not a Points enumerator
     pt2w = pt3d;           //  ok: both are objects of Points enum type

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.

注意把 3 赋给 Points 对象是非法的,即使 3 与一个 Points 枚举成员相关联。

Team LiB
Previous Section Next Section