c++11 class enum

1.简介

以往的enum类型将枚举成员的作用域暴露在枚举变量之外,用户不需要指定枚举类型就可以直接使用枚举的内容,这就有可能会造成名字的冲突,为了解决该你问题,C++11引入了强类型的枚举类型(strongly typed enums ).

2.旧风格的enum

[cpp]  view plain  copy
  1. #include "stdafx.h"  
  2. #include <iostream>  
  3. using namespace std;  
  4.   
  5. enum OldEnum  
  6. {  
  7.     First,  
  8.     Second,  
  9.     Third,  
  10. };  
  11.   
  12. int _tmain(int argc, _TCHAR* argv[])  
  13. {  
  14.     int nSelect = Second;  //enum cast to int  
  15.   
  16.     switch( nSelect )  
  17.     {  
  18.     case First:  //enum cast to int  
  19.         cout<<"Select first"<<endl;  
  20.         break;  
  21.     case Second:  
  22.         cout<<"Select second"<<endl;  
  23.         break;  
  24.     case OldEnum::Third:    //也支持通过enum限定  
  25.         cout<<"Select third"<<endl;  
  26.         break;  
  27.     default:  
  28.         cout<<"Select none"<<endl;  
  29.         break;  
  30.           
  31.     }  
  32.   
  33.     system("pause");  
  34.     return 0;  
  35. }  

由上述示例可以看出,旧风格的enum数据类型支持直接对enum成员的使用,例如First 同时也支持通过enum 名字的限定,例如,OldEnum::Third. 但后者不是强制的要求。旧风格的enum会自动根据需求转换成整数类型。


由于没有强制性的名字限定的要求,容易造成枚举类型名字的冲突,例如:

[cpp]  view plain  copy
  1. #include "stdafx.h"  
  2. #include <iostream>  
  3. using namespace std;  
  4.   
  5. enum OldEnum  
  6. {  
  7.     First,  
  8.     Second,  
  9.     Third,  
  10. };  
  11.   
  12. enum OldEnum2  
  13. {  
  14.     MyFirst = 1,  
  15.     Second,  
  16.     My_Third,  
  17. };  
  18. int _tmain(int argc, _TCHAR* argv[])  
  19. {  
  20.     int nSelect = Second;  //error C2365: 'Second' : redefinition; previous definition was 'enumerator'  
  21.   
  22.     system("pause");  
  23.     return 0;  
  24. }  
OldEnum 和 OldEnum2 都定义了枚举成员 Second,在使用时由于不需要名字的限定,编译器无法知道具体使用哪一个枚举成员,会产生编译错误。


3.强类型enum(C++11)

强类型enum数据类型在定义时和旧风格不同,需要添加 class 关键字。

[cpp]  view plain  copy
  1. #include "stdafx.h"  
  2. #include <iostream>  
  3. using namespace std;  
  4.   
  5. enum class StrongTypeEnum  
  6. {  
  7.     First,  
  8.     Second,  
  9.     Third,  
  10. };  
  11.   
  12. enum class StrongTypeEnum2  
  13. {  
  14.     MyFirst = 1,  
  15.     Second,  
  16.     My_Third,  
  17. };  
  18. int _tmain(int argc, _TCHAR* argv[])  
  19. {  
  20.     StrongTypeEnum strongEnum1;  
  21.     strongEnum1 = StrongTypeEnum::First;  // OK  
  22.   
  23.     //error C2065: 'First' : undeclared identifier  
  24.     strongEnum1 = First;    
  25.       
  26.     //error C2440: 'initializing' : cannot convert from 'StrongTypeEnum' to 'int'  
  27.     int nStrong = StrongTypeEnum::Second;   
  28.   
  29.     StrongTypeEnum2 strongEnum2;  
  30.   
  31.     //error C2440: '=' : cannot convert from 'StrongTypeEnum' to 'StrongTypeEnum2'  
  32.     strongEnum2 = StrongTypeEnum::First;  
  33.   
  34.     switch (strongEnum1)  
  35.     {  
  36.     case StrongTypeEnum::First:  
  37.         break;  
  38.     case StrongTypeEnum::Second:  
  39.         break;  
  40.     case StrongTypeEnum::Third:  
  41.         break;  
  42.     default:  
  43.         break;  
  44.     }  
  45.     system("pause");  
  46.     return 0;  
  47. }  
由上述示例可以看出强类型枚举的新特性:
(1)使用enum class 进行定义

(2)避免了名字冲突,StrongTypeEnum 和 StrongTypeEnum2都定义了成员 Second,但是由于各属于各自的空间,不会造成名字冲突

(3)使用枚举成员必须通过名字的限定

(4)枚举变量不能转换为整型

(5)不同的枚举变量之间不能互相赋值。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值