简体   繁体   English

什么时候使用枚举?

[英]When to use enums?

I'm currently reading about enums in C. I understand how they work, but can't figure out situations where they could be useful. 我目前正在阅读有关C语言中的枚举的信息。我了解它们的工作原理,但无法弄清它们可能有用的情况。 Can you give me some simple examples where the usage of enums is appropriate? 您能举一些简单的例子来说明枚举的用法吗?

They're often used to group related values together: 它们通常用于将相关值分组在一起:

enum errorcode {
    EC_OK = 0,
    EC_NOMEMORY,
    EC_DISKSPACE,
    EC_CONNECTIONBROKE,
    EC_KEYBOARD,
    EC_PBCK
};

Enums are just a way to declare constant values with more maintainability. 枚举只是一种声明具有更多可维护性的常量值的方法。 The benefits include: 好处包括:

  • Compilers can automatically assign values when they are opaque. 编译器不透明时可以自动分配值。
  • They mitigate programmers doing bad things like int monday = SUNDAY + 1 . 它们减轻了程序员做int monday = SUNDAY + 1类的坏事情的机会。
  • They make it easy to declare all of your related constants in a single spot. 它们使在同一位置声明所有相关常量变得容易。

Use them when you have a finite list of distinct, related values, as in the suits of a deck of cards. 当您有一系列明确的,相关的值的有限列表时,请使用它们,例如在一副纸牌中。 Avoid them when you have an effectively unbounded list or a list that could often change, as in the set of all car manufacturers. 当您拥有有效无界的列表或经常更改的列表时(如所有汽车制造商一样),请避免使用它们。

Sometime you want to express something that is finite and discrete. 有时您想表达有限且离散的内容。 An example from the GNU C Programming tutorial are compass directions. GNU C编程教程中的一个示例是指南针方向。

enum compass_direction
{
  north,
  east,
  south,
  west
};

Another example, where the ability of enums to correspond to integers comes in handy, could be status codes. 枚举对应于整数的功能很方便的另一个示例可能是状态码。 Usually you start the OK code with 0, so it can be used in if constructs. 通常,您以0开始OK代码,因此可以在if构造中使用它。

The concept behind an enum is also sometimes called an "enumerated type". 枚举背后的概念有时也称为“枚举类型”。 That is to say, it's a type all of whose possible values are named and listed as part of the definition of the type. 也就是说,它是一种类型,其所有可能的值都被命名并列为该类型定义的一部分。

C actually diverts from that a bit, since if a and b are values in an enum, then a|b is also a valid value of that type, regardless of whether it's listed and named or not. C实际上偏离了这一点,因为如果ab是枚举中的值,那么a|b也是该类型的有效值,而不管其是否被列出和命名。 But you don't have to use that fact, you can use an enum just as an enumerated type. 但是您不必使用这个事实,您可以将枚举用作枚举类型。

They're appropriate whenever you want a variable whose possible values each represent one of a fixed list of things. 每当您想要一个变量,每个变量的可能值代表固定的事物列表之一时,它们就很合适。 They're also sometimes appropriate when you want to define a bunch of related compile-time constants, especially since in C (unlike C++), a const int is not a compile-time constant. 当您想定义一堆相关的编译时常量时,它们有时也很合适,尤其是因为在C语言中(不同于C ++), const int不是编译时常量。

I think that their most useful properties are: 我认为它们最有用的属性是:

1) You can concisely define a set of distinct constants where you don't really care what the values are as long as they're different. 1)您可以简明地定义一组不同的常量,只要它们不同就不必在乎什么值。 typedef enum { red, green, blue } color; is better than: 优于:

#define red   0
#define green 1
#define blue  2

2) A programmer, on seeing a parameter / return value / variable of type color , knows what values are legal and what they mean (or anyway knows how to look that up). 2)程序员在看到参数/返回值/ color类型的变量时,知道哪些值合法以及它们的含义(或者无论如何都知道如何查找)。 Better than making it an int but documenting "this parameter must be one of the color values defined in some header or other". 比将其设置为int更好,但要说明“此参数必须是某些标头或其他标头中定义的颜色值之一”。

3) Your debugger, on seeing a variable of type color , may be able to do a reverse lookup, and give red as the value. 3)您的调试器在看到类型为color的变量时,可以进行反向查找,并将red作为值。 Better than you looking that up yourself in the source. 比您在源代码中查找自己要好得多。

4) The compiler, on seeing a switch of an expression of type color , might warn you if there's no default and any of the enumerated values is missing from the list of case s. 4)如果没有default并且case列表中缺少任何枚举值,则编译器在看到color类型的表达式switch时可能会警告您。 That helps avoid errors when you're doing something different for each value of the enum. 当您为枚举的每个值执行不同的操作时,这有助于避免错误。

Enums are primarily used because they are easier to read than integer numbers to programmers. 主要使用枚举,因为对于程序员而言,它们比整数更易于阅读。 For example, you can create an enum that represents dayy in a week. 例如,您可以创建一个代表一周中每一天的枚举。

enum DAY {Monday, Tuesday....}

Monday is equivalent to integer 0, Tuesday = Monday + 1 etc. 星期一等于整数0,星期二=星期一+ 1,依此类推。

So instead of integers thta represent each week day, you can use a new type DAY to represent this same concept. 因此,您可以使用新的类型DAY代替同一星期的概念,而不是代表每个星期的整数。 It is easier to read for the programmer, but it means the same to the compiler. 对于程序员而言,它更容易阅读,但对编译器而言,含义相同。

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

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