Do we really need quot;enum classquot; in C++11?(我们真的需要“枚举类吗?在 C++11 中?)
问题描述
当我们有时,
struct E { enum E_ { HELLO }; }; // 'E' is inheritable
那我们为什么需要,
enum class E { HELLO }; // 'E' is not inheritable
IMO 2nd 版本没有提供比 1st 多的功能.我不认为引入 enum class
只是为了节省 2 个花括号 {};
!我是否遗漏了任何重要方面?
IMO 2nd version doesn't offer more features than the 1st. I don't think that enum class
is introduced just to save 2 curly braces {};
! Am I missing any important aspect ?
作为一个小问题,除了语法之外,enum class
和 enum struct
之间是否有任何区别(因为两者都有 public
访问说明符)?
As a minor question, is there any difference between enum class
and enum struct
other than syntax (because both have public
access specifier) ?
推荐答案
除了已经提到的之外,enum class
的一个优点是更好的类型安全 - enum class
枚举器不会隐式转换为整数.
Besides what was already mentioned, an advantage of enum class
is better type safety - the enum class
enumerators don't convert implicitly to integers.
这篇关于我们真的需要“枚举类"吗?在 C++11 中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:我们真的需要“枚举类"吗?在 C++11 中?


基础教程推荐
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- CString 到 char* 2021-01-01
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 初始化列表*参数*评估顺序 2021-01-01
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16