我可以用枚举变量做什么?
当我声明这样的枚举变量时:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
在内部,
enum
被视为只能保存有限范围的值的整数。在这种情况下,常量RED
、GREEN
、BLUE
、...将被定义并且等于0
代码>、<代码>1、<代码>2、...(分别)。变量colors
可以用在任何可以使用int
的地方。您可以使用++
等运算符来迭代颜色列表。声明enumpaint_colorscolors
和intcolors
之间的唯一区别是枚举变量can应该仅被分配一个的枚举常量。与使用
#define
创建一系列常量以及使用普通int
表示颜色
相比,这给您带来了一些好处。首先,一些调试器可以检测到colors
是枚举类型,并将显示枚举常量的名称而不是数值。更重要的是,这可以添加额外的类型检查层。 C 标准不要求这样做,但某些编译器会检查并确保分配给枚举类型变量的值对应于枚举常量之一。
从心理上来说,您几乎可以认为这类似于这样的说法:
变量被视为 int,但显式赋予它不同的类型有助于阐明变量是什么以及它的用途。
Internally, an
enum
is treated as an integer that can only hold a limited range of values. In this case, the constantsRED
,GREEN
,BLUE
, ... will be defined and will be equal to0
,1
,2
, ... (respectively). The variablecolors
can be used anywhere anint
can be used. You can use operators like++
to iterate through the list of colors. The only difference between declaringenum paint_colors colors
andint colors
is that the enumerated variablecanshould 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 normalint
forcolors
. First, some debuggers can detect thatcolors
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:
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.实际上,您所做的就是声明一个与枚举定义的其余部分内联的变量。它相当于:
通常,您会看到与定义相关联的
typedef
:这使您可以像内置类型一样使用枚举:
所以您问题的答案是
colors< /code> 是一个
enum Paint_colors
类型的变量,可以以您认为适合此类变量的任何方式使用:等等。
Really what you're doing there is declaring a variable inline with the rest of the definition of the enumeration. It's equivalent to:
Often, you'll see a
typedef
associated with the definition:Which lets you use the enumeration more like the built in types:
So the answer to your question is that
colors
is a variable of typeenum paint_colors
, and can be used any way you think is appropriate for such a variable:And so on.
在C中,枚举类型变量是可以迭代的,如++、+;但这在C++中是不允许的;
可以用C编译,但不能用C++编译;
In C, the enum type variable can be iterated, such as ++, +; But this is not allowed in C++;
can be compiled in C, but not in C++;
你可以画一个过山车!
You could paint a roller coaster!
当您有一组给定的可能值(其中每个值都有一个有意义的名称)时,枚举会很有帮助。在其他地方,您引用枚举值,而不是“幻数”,这在其他代码中可能变得毫无意义。
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.
这是您所做的枚举类型声明。
具体回答您的问题,您可以像往常一样声明一个 int 变量,
并对 colors 变量执行所有操作,就像您对声明所做的那样。它不会对操作部分产生任何影响。无论是在枚举器常量值之外分配整数值,
唯一的区别是使代码更具可读性和更好理解性,因为枚举类型变量向程序员(特别是调试器)表明了其用途。取决于你;)
This is the enumeration type declaration you have made.
Answering specifically to your question, you can declare an int variable as usual,
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,
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 ;)
我发现枚举在调试过程中特别有用。如果您仅使用#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.