简体   繁体   English

将值分配给枚举

[英]Assigning values to enum

While doing a review of some older code, I notice the following two strange constructions using enum (two different files/classes/namespaces, just putting them together here): 在回顾一些旧的代码时,我注意到以下两个使用enum的奇怪结构(两个不同的文件/类/命名空间,只是将它们放在一起):

enum FirstEnum
   {
    A_CHOICE
   ,ANOTHER_CHOICE=1
   ,YET_SOME_OTHER_CHOICE
   };

enum SecondEnum
   {
    FIRST_CHOICE
   ,SECOND_CHOICE
   ,THIRD_CHOICE
   ,DEFAULT_CHOICE=SECOND_CHOICE
   };

I think both constructions are wrong. 我认为这两种结构都是错误的。

The first one assigns a value to one of the choices, but not to the others, meaning that things might go wrong if new choices are added. 第一个为其中一个选项赋值,但不为其他选项赋值,这意味着如果添加新选项,事情可能会出错。

In the second case, we end up with two enumeration elements having the same underlying value. 在第二种情况下,我们最终得到两个具有相同基础值的枚举元素。

Is there any reason why the C++ standard allows both constructions? 有没有理由说C ++标准允许这两种结构?

(using Visual Studio 2010) (使用Visual Studio 2010)

The first one assigns a value to one of the choices, but not to the others, meaning that things might go wrong if new choices are added. 第一个为其中一个选项赋值,但不为其他选项赋值,这意味着如果添加新选项,事情可能会出错。

I don't know what you mean by "go wrong". 我不知道你的意思是“出错”。 It's well-defined that if you don't specify a value for an enumerator, its value is one more than the previous (or zero, if it's the first). 它定义明确,如果你没有为枚举器指定一个值,它的值比前一个值多一个(如果它是第一个,则为零)。

In the second case, we end up with two enumeration elements having the same underlying value. 在第二种情况下,我们最终得到两个具有相同基础值的枚举元素。

Yes we do. 是的我们做到了。 That would be wrong if enumerations were supposed to be a set of unique values but (in C++) they aren't. 如果枚举应该是一组唯一值,但是(在C ++中)它们不是,那将是错误的。

Is there any reason why the C++ standard allows both constructions? 有没有理由说C ++标准允许这两种结构?

Because, in C++, an enumeration is simply a way to declare a set of related, named, constant values. 因为,在C ++中,枚举只是一种声明一组相关的,命名的常量值的方法。 It doesn't try to restrict what values they can take. 它不会试图限制它们可以采取的值。

This article from Microsoft should help: Microsoft的这篇文章应该有所帮助:

http://msdn.microsoft.com/en-us/library/2dzy4k6e(v=VS.80).aspx http://msdn.microsoft.com/en-us/library/2dzy4k6e(v=VS.80).aspx

The first one assigns a value to one of the choices, but not to the others 第一个为其中一个选项分配值,但不为其他选择分配值

By default, the first enumerator has a value of 0, and each successive enumerator is one larger than the value of the previous one, unless you explicitly specify a value for a particular enumerator. 默认情况下,第一个枚举数的值为0,每个连续的枚举数大于前一个枚举数的值,除非您明确指定特定枚举数的值。

In the second case, we end up with two enumeration elements having the same underlying value. 在第二种情况下,我们最终得到两个具有相同基础值的枚举元素。

Enumerators needn't have unique values within an enumeration. 枚举器不需要在枚举中具有唯一值。 The name of each enumerator is treated as a constant and must be unique within the scope where the enum is defined. 每个枚举数的名称都被视为常量,并且在定义枚举的范围内必须是唯一的。

The article includes examples of how these features could be taken advantage of. 本文包括如何利用这些功能的示例。

I don't have a quote from the standard for you, but enums are specified such that uninitialized values take on a value one larger than the value preceding them. 我没有标准的引用,但是指定了枚举,使得未初始化的值的值大于它们之前的值。

In the FirstEnum, YET_SOME_OTHER_CHOICE would therefore be 2 (ANOTHER_CHOICE+1). 因此,在FirstEnum中,YET_SOME_OTHER_CHOICE将为2(ANOTHER_CHOICE + 1)。 It is also perfectly legal to have multiple equivalent values within an enum. 在枚举中具有多个等效值也是完全合法的。

The first one assigns a value to one of the choices, but not to the others, meaning that things might go wrong if new choices are added. 第一个为其中一个选项赋值,但不为其他选项赋值,这意味着如果添加新选项,事情可能会出错。

What might go wrong? 可能出什么问题? Sure, if somebody changes the first example to 当然,如果有人将第一个例子改为

enum FirstEnum
   {
    A_CHOICE              //0
   ,A_THIRD_CHOICE        //1
   ,ANOTHER_CHOICE=1      //1
   ,YET_SOME_OTHER_CHOICE //2
   };

Then yes, they will get problems if they didn't expect two values to be the same in the enum. 然后是的,如果他们不期望枚举中的两个值相同,他们就会遇到问题。 The same if somebody had #define d these values, and accidentally made two of them the same. 如果某人有#define d这些值,并且意外地使其中两个相同,则相同。

And for your second example, the names of the values in the enumeration give a hint as to why that is useful. 对于您的第二个示例,枚举中的值的名称提供了有关为何有用的提示。 You can have a default value for variables of type SecondEnum stored in the definition of SecondEnum , allowing you to do things like 您可以为SecondEnum定义中存储的SecondEnum类型的变量设置默认值,允许您执行类似的操作

SecondEnum var = DEFAULT_CHOICE;

without the need for #define s or constants that are closely coupled to the enum definition but aren't part of it. 不需要与枚举定义紧密耦合但不属于它的#define或常量。

Is there any reason why the C++ standard allows both constructions? 有没有理由说C ++标准允许这两种结构?

I'm not on the standards committee, but if I were to guess, it's because both constructions are useful for programmers. 我不是标准委员会,但如果我猜测,那是因为这两种结构对程序员都很有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM