我可以用枚举变量做什么?

发布于 2024-08-26 19:50:06 字数 161 浏览 3 评论 0原文

当我声明这样的枚举变量时:

enum paint_colors { RED, GREEN, BLUE, ...} colors;

colors 变量有用吗?如果是这样,我能用它做什么?

提前致谢。

When I declare a enum variable like this:

enum paint_colors { RED, GREEN, BLUE, ...} colors;

is the colors variable useful? If so, what can I do with it?

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(7

梦境 2024-09-02 19:50:06

在内部,enum 被视为只能保存有限范围的值的整数。在这种情况下,常量REDGREENBLUE、...将被定义并且等于0代码>、<代码>1、<代码>2、...(分别)。变量colors可以用在任何可以使用int的地方。您可以使用 ++ 等运算符来迭代颜色列表。声明enumpaint_colorscolorsintcolors之间的唯一区别是枚举变量can应该被分配一个的枚举常量。

与使用 #define 创建一系列常量以及使用普通 int 表示 颜色 相比,这给您带来了一些好处。首先,一些调试器可以检测到colors是枚举类型,并将显示枚举常量的名称而不是数值。

更重要的是,这可以添加额外的类型检查层。 C 标准不要求这样做,但某些编译器会检查并确保分配给枚举类型变量的值对应于枚举常量之一。

从心理上来说,您几乎可以认为这类似于这样的说法:

#define RED    0
#define GREEN  1
#define BLUE   2
typedef int paint_colors;
paint_colors colors;

变量被视为 int,但显式赋予它不同的类型有助于阐明变量是什么以及它的用途。

Internally, an enum is treated as an integer that can only hold a limited range of values. In this case, the constants RED, GREEN, BLUE, ... will be defined and will be equal to 0, 1, 2, ... (respectively). The variable colors can be used anywhere an int can be used. You can use operators like ++ to iterate through the list of colors. The only difference between declaring enum paint_colors colors and int colors is that the enumerated variable can should only be assigned one of the enumerated constants.

This gives you several benefits over using #define to create a series of constants and using a normal int for colors. First, some debuggers can detect that colors is an enumerated type and will display the name of the enumerated constant instead of a numeric value.

More importantly, this can add an additional layer of type checking. It is not required by the C standard, but some compilers check and make sure that values assigned to a variable of enumerated type correspond to one of the enumerated constants.

Mentally, you can almost think of this is similar to saying:

#define RED    0
#define GREEN  1
#define BLUE   2
typedef int paint_colors;
paint_colors colors;

The variable is treated like an int, but explicitly giving it a different type helps to clarify what the variable is and what it is used for.

(り薆情海 2024-09-02 19:50:06

实际上,您所做的就是声明一个与枚举定义的其余部分内联的变量。它相当于:

enum paint_colors { RED, GREEN, BLUE };
enum paint_colors colors;

通常,您会看到与定义相关联的 typedef

typedef enum _paint_colors { RED, GREEN, BLUE } paint_colors;

这使您可以像内置类型一样使用枚举:

paint_colors color;

所以您问题的答案是 colors< /code> 是一个 enum Paint_colors 类型的变量,可以以您认为适合此类变量的任何方式使用:

colors = RED;
if (colors != GREEN)
{
    colors = BLUE;
}

等等。

Really what you're doing there is declaring a variable inline with the rest of the definition of the enumeration. It's equivalent to:

enum paint_colors { RED, GREEN, BLUE };
enum paint_colors colors;

Often, you'll see a typedef associated with the definition:

typedef enum _paint_colors { RED, GREEN, BLUE } paint_colors;

Which lets you use the enumeration more like the built in types:

paint_colors color;

So the answer to your question is that colors is a variable of type enum paint_colors, and can be used any way you think is appropriate for such a variable:

colors = RED;
if (colors != GREEN)
{
    colors = BLUE;
}

And so on.

伴梦长久 2024-09-02 19:50:06

在C中,枚举类型变量是可以迭代的,如++、+;但这在C++中是不允许的;

enum WEEKDAY {MON, TUE, WED, THU, FRI, SAT, SUN, END};
for(WEEKDAY day = 0; day < END; day++){
    printf("day index = %d\n", day);
}

可以用C编译,但不能用C++编译;

In C, the enum type variable can be iterated, such as ++, +; But this is not allowed in C++;

enum WEEKDAY {MON, TUE, WED, THU, FRI, SAT, SUN, END};
for(WEEKDAY day = 0; day < END; day++){
    printf("day index = %d\n", day);
}

can be compiled in C, but not in C++;

枕头说它不想醒 2024-09-02 19:50:06

你可以画一个过山车!

typedef struct {
  Color car;
  Color door_handle;
  Color wheels;
} RollerCoasterColors;

int main(void) {
  RollerCoasterColors roller_coaster_colors;
  roller_coaster_colors.car = RED;
  roller_coaster_colors.door_handle = YELLOW;
  roller_coaster_colors.wheels = PURPLE;

  // now paint_roller_coaster() knows what colour to paint each part!
  paint_roller_coaster(roller_coaster_colors);
}

You could paint a roller coaster!

typedef struct {
  Color car;
  Color door_handle;
  Color wheels;
} RollerCoasterColors;

int main(void) {
  RollerCoasterColors roller_coaster_colors;
  roller_coaster_colors.car = RED;
  roller_coaster_colors.door_handle = YELLOW;
  roller_coaster_colors.wheels = PURPLE;

  // now paint_roller_coaster() knows what colour to paint each part!
  paint_roller_coaster(roller_coaster_colors);
}
久而酒知 2024-09-02 19:50:06

当您有一组给定的可能值(其中每个值都有一个有意义的名称)时,枚举会很有帮助。在其他地方,您引用枚举值,而不是“幻数”,这在其他代码中可能变得毫无意义。

An enum is helpful when you have a given set of possible values, where each value has a meaningful name. Elsewhere, you refer to the enum values, rather than "magic numbers", which can become meaningless among other code.

夏至、离别 2024-09-02 19:50:06
enum paint_colors { RED, GREEN, BLUE, ...} colors;

这是您所做的枚举类型声明。
具体回答您的问题,您可以像往常一样声明一个 int 变量,

enum paint_colors { RED, GREEN, BLUE, ...};
int colors;

并对 colors 变量执行所有操作,就像您对声明所做的那样。它不会对操作部分产生任何影响。无论是在枚举器常量值之外分配整数值,

colors=1676;    //Valid in both of our case

唯一的区别是使代码更具可读性和更好理解性,因为枚举类型变量向程序员(特别是调试器)表明了其用途。取决于你;)

enum paint_colors { RED, GREEN, BLUE, ...} colors;

This is the enumeration type declaration you have made.
Answering specifically to your question, you can declare an int variable as usual,

enum paint_colors { RED, GREEN, BLUE, ...};
int colors;

and perform all operations on colors variable same as you would have done with your declaration. It will not make any difference in operation part. Be it assigning a integer value outside the enumerators constant value,

colors=1676;    //Valid in both of our case

The only difference it makes is making the code more readable and better to understand because enum type variable indicates its purpose to the programmer(specially debugger). Depends on you ;)

贪了杯 2024-09-02 19:50:06

我发现枚举在调试过程中特别有用。如果您仅使用#define 定义用作某些变量值的常量,调试器将向您显示数字。通过枚举,它可以显示名称。

如果您需要单个变量的一组可能值(常量),您可以按照您显示的方式定义它。

I find enums especially useful during debugging. If you just use #define to define constants used as values for some variable the debugger will show you the numer. With enum it can show you the names.

And if you need a set of possible values (constants) for a single variable you can define it the way you showed.

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