关于 C 中枚举的使用:是否可以更改枚举成员的值?
所以这似乎是一个愚蠢的问题,但我想知道是否有任何方法可以更改 C 中枚举成员的值。
例如,假设我有:
enum yourcolor{red = 43, blue = 54, green = 89};
enum yourcolor blue-green = red*blue*green;
是否有一种方法可以更改红色、蓝色的值代码中稍后会出现绿色?
我在想这样的事情:
yourcolor.red = 56; etc...
我知道你应该使用结构来做这样的事情,但这只是我今天在想的事情,我找不到答案。
任何帮助将不胜感激。
So this might seem like a silly quesiton, but I was wondering if there was any way to change the values of members of an enum in C.
For instance, say I have:
enum yourcolor{red = 43, blue = 54, green = 89};
enum yourcolor blue-green = red*blue*green;
would there be a way to change the values of red, blue and green later on in the code?
I was thinking something like:
yourcolor.red = 56; etc...
I know you should use a struct for something like this, but this is just something I was thinking about today, and I can't find an answer to.
Any help would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
简短的回答是“不”——在定义枚举常量后,您不能更改它的值。
从语义上讲,这样做没有任何意义。使用枚举类型背后的全部意义在于抽象出底层表示——您关心的是颜色是红色,而不是 43 或 56 等。换句话说,如果
red
是 43 或 56 很重要,那么您不应该使用枚举。当您创建枚举类型的对象时,您基本上是在说该对象只能采用这些枚举值之一;它可以是
红色
、蓝色
或绿色
。如果要表示辅助颜色,则需要为这些辅助颜色定义枚举常量。您可以根据包含主项的表达式来定义这些辅助项。以下是许多可能的编码之一:显示的编码很方便;它们展示了如何从原色构建出二次色。它们也完全任意;您可以将类型定义为
,并且完全不用担心底层值(分别为 0、1、2、3、4、5、6 和 7)。
通过创建此类型,您暗示
enum color
类型的对象应该仅采用以下值之一:black
、红色
、蓝色
、绿色
、洋红色
、青色
、黄色
,或white
(无论底层整数如何 价值)。当然,C 对此并不严格:枚举类型的对象可以保存任意整数值,即使该值没有出现在枚举常量列表中。但是这样做就打破了概念类型隐含的模型。
The short answer is "no" -- you cannot change the value of an enumeration constant after it has been defined.
Semantically, it would not make any sense to do so. The whole point behind using enumerated types is to abstract away the underlying representation -- you care that a color is
red
, not 43 or 56 or whatever. Put another way, if it matters thatred
is 43 or 56, then you shouldn't be using an enumeration.When you create an object of an enumerated type, you're basically saying that the object can only take on one of those enumerated values; it can either be
red
orblue
orgreen
. If you want to represent secondary colors, you would want to define enumeration constants for those secondaries. You can define those secondaries in terms of expressions containing the primaries. The following is one of many possible encodings:The encodings shown are convenient; they show how secondary colors are built from the primaries. They're also completely arbitrary; you could define the type as
and not worry at all about the underlying values (which will be 0, 1, 2, 3, 4, 5, 6, and 7, respectively).
By creating this type, you're implying that an object of type
enum color
should only take on one of the valuesblack
,red
,blue
,green
,magenta
,cyan
,yellow
, orwhite
(irrespective of the underlying integer value).Of course, C"s not exactly strict about this: an object of enumerated type can hold an arbitrary integer value, even if that value doesn't appear in the list of enumeration constants. But by doing so, you're breaking the conceptual model implied by the type.
不,这是不可能的。好吧,它可以在不同的翻译单元中工作,尽管这可能不是您想要的。
不过,您可以使用变量:
这些可以更改。
No, it's not possible. Well, it would work in different translation units, although that probably isn't what you want.
You can use a variable though:
These can be changed.
不。
enum
中的标识符被声明为常量。来自 C99 (强调我的):
注意,在同一部分的后面(斜体是我的):
更一般地说,我会质疑这样一个功能的动机。根据定义,枚举是某个集合的所有成员的完整列表,其值在正常情况下可能不应该改变。
No. The identifiers in an
enum
are declared as constants.From C99 (emphasis mine):
Note, later in the same section (italics mine):
More generally, I'd question the motivation for such a feature. An enumeration, by definition, is the complete listing of all members of some set, the values of which probably shouldn't be changing in normal circumstances.
我认为不太可能。枚举值很可能在编译时被常量静态替换,因此分配给它们的行为将是古怪的(最坏的情况),或者被拒绝为错误(最好的情况)。
Very unlikely, I think. Enum values are likely replaced statically by constants at compile time, and so the behaviour of assigning to them is going to be wacky (at worst), or rejected as an error (at best).
这似乎没有意义。枚举实际上是常量,我假设它们被硬编码到机器指令中。
这意味着程序无法知道它们何时发生变化。如果你在一个非线性的程序中改变它,当你跳回来时会发生什么?就像在循环或 goto 语句中一样?
Doesn't seem like this would make sense. Enums are in fact constants and I assume they get hardcoded into machine instructions.
This means the program has no way of knowing when they change. If you changed it in a program that isn't linear, what would happen when you jumped back? Like in a loop or goto statement?