我们真的需要“枚举类”在C ++ 11? [英] Do we really need "enum class" in C++11?

查看:117
本文介绍了我们真的需要“枚举类”在C ++ 11?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我们有,

  struct E {enum E_ {HELLO}; }; //'E'is inheritable 

那么我们为什么需要,

 枚举类E {HELLO}; //'E'不可继承

IMO第2版不提供比第1版更多的功能。我不认为枚举类只是为了保存2个大括号 {}; !我缺少任何重要的方面?



作为一个小问题,枚举类和<$除了语法(因为都有 public 访问说明符)?

枚举类枚举不会隐式转换为整数。


When we have,

struct E { enum E_ { HELLO }; }; // 'E' is inheritable

then why do we need,

enum class E { HELLO };  // 'E' is not inheritable

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 ?

As a minor question, is there any difference between enum class and enum struct other than syntax (because both have public access specifier) ?

解决方案

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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆