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

 

發佈了94 篇原創文章 · 獲贊 142 · 訪問量 33萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章