C++ 11 新特性 之 Enum Class

C++ 11起引入的 enum class相对于传统的enum有了很多变化,主要是针对传统 enum 在编程过程中出现的值类型名称作用域、enum类型安全问题进行了改良.

一、传统enum类型    

先来看看传统enum在编程过程中可能遇到的一些问题:

    1、两个 enum 类型声明时不能有相同值名称声明:

#include <bits/stdc++.h> 
using namespace std; 

int main() 
{ 
	// Defining enum1 Gender 
	enum Gender { Male, Female }; 

	// Defining enum2 Gender2 with same values 
	// This will throw error 
	enum Gender2 { Male, Female }; 

	// Creating Gender type variable 
	Gender gender = Male; 
	Gender2 gender2 = Female; 

	cout << gender << endl << gender2; 

	return 0; 
} 

编译报错:

prog.cpp:13:20: error: redeclaration of 'Male'
     enum Gender2 { Male,
                    ^
prog.cpp:8:19: note: previous declaration 'main()::Gender Male'
     enum Gender { Male,
                   ^
prog.cpp:14:20: error: redeclaration of 'Female'
                    Female };
                    ^
prog.cpp:9:19: note: previous declaration 'main()::Gender Female'
                   Female };
                   ^
prog.cpp:18:23: error: cannot convert 'main()::Gender' 
to 'main()::Gender2' in initialization
     Gender2 gender2 = Female;

2、enum类型内定义的值名称作用域:

#include <bits/stdc++.h> 
using namespace std; 

int main() 
{ 
	// Defining enum1 Gender 
	enum Gender { Male, Female }; 

	// Creating Gender type variable 
	Gender gender = Male; 

	// creating a variable Male 
	// this will throw error 
	int Male = 10; 

	cout << gender << endl; 

	return 0; 
} 

编译报错:

prog.cpp: In function 'int main()':
prog.cpp:16:9: error: 'int Male' redeclared as different kind of symbol
     int Male = 10;
         ^
prog.cpp:8:19: note: previous declaration 'main()::Gender Male'
     enum Gender { Male,
                   ^

3、类型安全

#include <bits/stdc++.h> 
using namespace std; 

int main() 
{ 
	// Defining enum1 Gender 
	enum Gender { Male, Female }; 

	// Defining enum2 Color 
	enum Color { Red, Green }; 

	// Creating Gender type variable 
	Gender gender = Male; 
	Color color = Red; 

	// Upon comparing gender and color 
	// it will return true as both have value 0 
	// which should not be the case actually 
	if (gender == color) 
		cout << "Equal"; 

	return 0; 
} 

编译警告:

prog.cpp: In function 'int main()':
prog.cpp:23:19: warning: comparison between 'enum main()::Gender'
and 'enum main()::Color' [-Wenum-compare]
     if (gender == color)                ^

二、enum class 类型

    enum class也叫scoped enumerations, 它是类型安全的并且用于显式作用域. Enum class 不允许隐式的int类型转换, 而且不同类型的枚举变量不可以直接比较.

    enum class 的声明与变量定义:

// Declaration
enum class EnumName{ Value1, Value2, ... ValueN};

// Initialisation
EnumName ObjectName = EnumName::Value;

  使用示例:

// C++ program to demonstrate working 
// of Enum Classes 

#include <iostream> 
using namespace std; 

int main() 
{ 

	enum class Color { Red, Green, Blue }; 
	enum class Color2 { Red, Black, White }; 
	enum class People { Good, Bad }; 

	// An enum value can now be used 
	// to create variables 
	int Green = 10; 

	// Instantiating the Enum Class 
	Color x = Color::Green; 

	// Comparison now is completely type-safe 
	if (x == Color::Red) 
		cout << "It's Red\n"; 
	else
		cout << "It's not Red\n"; 

	People p = People::Good; 

	if (p == People::Bad) 
		cout << "Bad people\n"; 
	else
		cout << "Good people\n"; 

	// gives an error 
	// if(x == p) 
	// cout<<"red is equal to good"; 

	// won't work as there is no 
	// implicit conversion to int 
	// cout<< x; 

	cout << int(x); 

	return 0; 
} 

执行程序得到的输出结果:

It's not Red
Good people
1

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值