c++中枚举数组的初始化

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24355872/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 00:42:17  来源:igfitidea点击:

initialization of enum array in c++

c++arraysenumsinitialization

提问by ddur

I'm learning c++ and doing some exercise from a book right now. I'm asked to write definition of enum which takes 3 values (forward, backward, stop) and array of 15 elements of this type. In this definition I have to set values of first 5 elements of this array. I did it without problems, but it made me think.. If I write:

我现在正在学习 C++ 并从一本书中做一些练习。我被要求编写枚举的定义,它采用 3 个值(向前、向后、停止)和这种类型的 15 个元素的数组。在这个定义中,我必须设置这个数组的前 5 个元素的值。我做到了没有问题,但它让我思考..如果我写:

enum move {FORWARD = 1, BACKWARD, STOP};
move array[15] {FORWARD, BACKWARD, STOP, STOP, STOP};

... first 5 elements will have values of 1, 2, 3, 3, 3 but all after that will have 0. How is it possible since "move" can't take values other than 1, 2 or 3? Why is the rest of array initialized anyway if I specified just first 5 fields? Or maybe it just means those are empty fields?

...前 5 个元素的值将是 1、2、3、3、3,但之后的所有元素的值都是 0。因为“移动”不能取 1、2 或 3 以外的值,这怎么可能?如果我只指定了前 5 个字段,为什么数组的其余部分仍然被初始化?或者也许这只是意味着那些是空的领域?

Please explain in simple way so beginner like me can understand :), thanks.

请以简单的方式解释,以便像我这样的初学者可以理解:),谢谢。

回答by dyp

Every enumeration has an underlying type, and supports a range of values. The underlying type is an integer type big enough to store the whole range of values. All integers in that range are valid values (see below) for an object of the enumeration type, even if there's no enumerator with that value. 0is in the range of values of any enumeration, hence initializing an object of enumeration type with 0is fine.

每个枚举都有一个基础类型,并支持一系列值。底层类型是一个足够大的整数类型,可以存储整个范围的值。该范围内的所有整数都是枚举类型对象的有效值(见下文),即使没有具有该值的枚举器。0在任何枚举的值范围内,因此初始化枚举类型的对象0是可以的。

Integer values can be converted to the type of the enumeration if they are in the range of values that this enumeration supports.

如果整数值在此枚举支持的值范围内,则可以将其转换为该枚举的类型。

If you initialize an aggregate (like an array) with braces {..}and provide less initializers than there are elements in the aggregate, the resulting elements will be value-initialized. For fundamental types (like int) and enumeration types, this means they will be initialized from the integer 0, converted to their respective type.

如果您使用大括号初始化聚合(如数组){..}并提供比聚合中元素少的初始值设定项,则生成的元素将是value-initialized。对于基本类型(如int)和枚举类型,这意味着它们将从 integer 初始化0,转换为它们各自的类型。

move array[6] {FORWARD, BACKWARD, STOP, STOP, STOP};
// equivalent to:
move array[6] {FORWARD, BACKWARD, STOP, STOP, STOP, static_cast<move>(0)};

回答by Vlad from Moscow

According to the C++ Standard enumerations with a non-fixed underlaying type have minimum value equal to zero if they were defined such a way that their enumerator with minimum value is not negative.

根据 C++ 标准,具有非固定底层类型的枚举的最小值等于 0,如果它们被定义为具有最小值的枚举数不为负。

In your code the enumerator with minimum value is FORWARD. Its value is not negative. So the minimum valid value of the enumeration is 0.

在您的代码中,具有最小值的枚举数是FORWARD. 它的价值不是负数。所以枚举的最小有效值为 0。

Take into account that according to the same C++ Stsandard

考虑到根据相同的 C++ Stsandard

It is possible to define an enumeration that has values not defined by any of its enumerators.

可以定义一个枚举,它的值不是由它的任何枚举器定义的。

As for the array as it has less initializers than there are elements in the array then all elements that have no an initializer will be zero-initialized. And zero is the valid value for the enumeration.

至于数组,因为它的初始化器比数组中的元素少,所以所有没有初始化器的元素都将被零初始化。零是枚举的有效值。

回答by ctzdev

Your enum moveis created explicitly with FORWARD = 1, BACKWARD = 2, STOP = 3.

enum move是使用FORWARD = 1, BACKWARD = 2, STOP = 3.

When you try to initialize your array, the rest of the values are zero because you have to give them an explicit value or else it defaults to zero in order to fill the array.

当您尝试初始化数组时,其余值为零,因为您必须给它们一个显式值,否则默认为零以填充数组。

There cannot be "empty memory" in an array and there cannot be a partially initialized array either.

数组中不能有“空内存”,也不能有部分初始化的数组。

In other words, enumvalues are an abstraction for intand therefore must be initialized as integers.

换句话说,enum值是对的抽象int,因此必须初始化为整数。