为什么人们在 C++ 中使用枚举? 作为常量,同时可以使用 const 吗?
当人们可以使用 const 时,为什么在 C++ 中使用枚举作为常量?
Why do people use enums in C++ as constants when they can use const
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
Bruce Eckel 在 Thinking in C++ 中给出了原因:
Bruce Eckel gives a reason in Thinking in C++:
枚举是不同的类型,因此您可以执行面向类型的操作,例如使用它们进行重载:
Enums are distinct types, so you can do type-oriented things like overloading with them:
枚举意味着一组相关常量,因此有关关系的附加信息必须在当前问题的模型中有用。
An enumeration implies a set of related constants, so the added information about the relationship must be useful in their model of the problem at hand.
处理模板元编程时也有一个历史原因。 某些编译器可以使用枚举中的值,但不能使用 static const int 来实例化类。
现在你可以用更明智的方式做到这一点:
另一个可能的原因是 static const int 可能在运行时为其保留内存,而枚举永远不会为其保留实际的内存位置,并且将在编译时间。 请参阅此相关问题。
There's a historical reason too when dealing with template metaprogramming. Some compilers could use values from an enum, but not a static const int to instantiate a class.
Now you can do it the more sensible way:
Another possible reason, is that a static const int may have memory reserved for it at runtime, whereas an enum is never going to have an actual memory location reserved for it, and will be dealt at compile time. See this related question.
我喜欢可以与枚举一起使用的自动行为,例如:
然后很容易循环直到最后,并且当添加新状态(或表示的任何状态)时,逻辑会适应。
添加一些东西...
循环适应...
I like the automatic behavior that can be used with enums, for example:
Then it is easy to loop until LAST, and when a new state (or whatever is represented) is added, the logic adapts.
Add something...
The loop adapts...
使用枚举时更具描述性。 考虑:
与
此外,枚举提供了更多的类型安全性,因为
Enums are more descriptive when used. Consider:
versus
In addition, enums give a bit more type-safety, because
在编译器供应商实施 ISO/IEC 14882:1998 C++ 标准之前,在类作用域中定义常量的代码会导致编译错误:
如果常量是整数类型,则一个笨拙的解决方法是在类范围内的枚举中定义它。班级:
Before compiler vendors implemented the ISO/IEC 14882:1998 C++ standard, this code to define a constant in a class scope resulted in a compile error:
If the constant is an integer type, a kludgy work around is define it in an enum inside the class:
枚举也可以用作类型名称。 因此,您可以定义一个采用枚举作为参数的函数,与将值定义为 const 变量并且函数仅接受“int”相比,这使得更清楚应该将哪些类型的值作为函数的参数给出作为一个论点。
考虑:
与:
enums also can be used as a type name. So you can define a function that takes an enum as a parameter, which makes it more clear what kinds of values should be given as arguments to the function, as compared to having the values defined as const variables and the function accepting just "int" as an argument.
Consider:
versus:
某些调试器在调试时会显示枚举名称而不是其值。 这非常有帮助。 我知道我宁愿看到
day_of_week = MONDAY
而不是day_of_week = 1
。Some debuggers will show the enumeration name instead of its value when debugging. This can be very helpful. I know that I would rather see
day_of_week = MONDAY
thanday_of_week = 1
.部分原因是较旧的编译器不支持真正的类常量的声明,
因此必须这样做。
因此,许多可移植库继续使用这种形式。
另一个原因是枚举可以用作一种方便的语法设备,将类常量组织成相关的和不相关的常量
。
It's partly because older compilers did not support the declaration of a true class constant
so had to do this
For this reason, many portable libraries continue to use this form.
The other reason is that enums can be used as a convenient syntactic device to organise class constants into those that are related, and those that are not
vs
使用枚举以简洁的方式记录有效的选择,并允许编译器强制执行它们。
如果他们使用枚举存储全局常量,例如 Pi,那么我不知道他们的目标是什么。
Using an enum documents the valid choices in a terse manner and allows the compiler to enforce them.
If they are using enum store global constants, like Pi, for example, then I don't know what their goal is.
原因之一是
const
需要更多的输入:...与...
One reason is that
const
requires more typing:...versus...