关于 C 中枚举的使用:是否可以更改枚举成员的值?

发布于 2024-12-19 01:54:38 字数 360 浏览 3 评论 0原文

所以这似乎是一个愚蠢的问题,但我想知道是否有任何方法可以更改 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

鹿! 2024-12-26 01:54:38

简短的回答是“不”——在定义枚举常量后,您不能更改它的值。

从语义上讲,这样做没有任何意义。使用枚举类型背后的全部意义在于抽象出底层表示——您关心的是颜色是红色,而不是 43 或 56 等。换句话说,如果 red 是 43 或 56 很重要,那么您不应该使用枚举。

当您创建枚举类型的对象时,您基本上是在说该对象只能采用这些枚举值之一;它可以是红色蓝色绿色。如果要表示辅助颜色,则需要为这些辅助颜色定义枚举常量。您可以根据包含主项的表达式来定义这些辅助项。以下是许多可能的编码之一:

enum color {black   = 0x00,              // binary 0000
            red     = 0x01,              // binary 0001
            blue    = 0x02,              // binary 0010
            green   = 0x04,              // binary 0100
            magenta = red | blue,        // binary 0011
            cyan    = blue | green,      // binary 0110 
            yellow  = red | green,       // binary 0101
            white   = red | blue | green // binary 0111
};

显示的编码很方便;它们展示了如何从原色构建出二次色。它们也完全任意;您可以将类型定义为

enum color {black, red, blue, green, magenta, cyan, yellow, white};

,并且完全不用担心底层值(分别为 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 that red 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 or blue or green. 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:

enum color {black   = 0x00,              // binary 0000
            red     = 0x01,              // binary 0001
            blue    = 0x02,              // binary 0010
            green   = 0x04,              // binary 0100
            magenta = red | blue,        // binary 0011
            cyan    = blue | green,      // binary 0110 
            yellow  = red | green,       // binary 0101
            white   = red | blue | green // binary 0111
};

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

enum color {black, red, blue, green, magenta, cyan, yellow, white};

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 values black, red, blue, green, magenta, cyan, yellow, or white (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.

浅唱ヾ落雨殇 2024-12-26 01:54:38

不,这是不可能的。好吧,它可以在不同的翻译单元中工作,尽管这可能不是您想要的。

不过,您可以使用变量:

typedef int color;
color red = 43;
color blue = 54;
color green = 89;

这些可以更改。

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:

typedef int color;
color red = 43;
color blue = 54;
color green = 89;

These can be changed.

风筝有风,海豚有海 2024-12-26 01:54:38

不。enum 中的标识符被声明为常量。

来自 C99 (强调我的):

枚举器列表中的标识符声明为常量
具有 int 类型,并且可以出现在任何允许的地方

注意,在同一部分的后面(斜体是我的):

每个枚举类型应与整数类型兼容。这
类型的选择是实现定义的,但应能够
代表枚举所有成员的值。

更一般地说,我会质疑这样一个功能的动机。根据定义,枚举是某个集合的所有成员的完整列表,其值在正常情况下可能不应该改变。

No. The identifiers in an enum are declared as constants.

From C99 (emphasis mine):

The identifiers in an enumerator list are declared as constants that
have type int and may appear wherever such are permitted

Note, later in the same section (italics mine):

Each enumerated type shall be compatible with an integer type. The
choice of type is implementation-defined, but shall be capable of
representing the values of all the members of the enumeration.

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.

节枝 2024-12-26 01:54:38

我认为不太可能。枚举值很可能在编译时被常量静态替换,因此分配给它们的行为将是古怪的(最坏的情况),或者被拒绝为错误(最好的情况)。

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).

回忆凄美了谁 2024-12-26 01:54:38

这似乎没有意义。枚举实际上是常量,我假设它们被硬编码到机器指令中。

这意味着程序无法知道它们何时发生变化。如果你在一个非线性的程序中改变它,当你跳回来时会发生什么?就像在循环或 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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文