C ++従来の列挙に比べ列挙クラスから導入11は多くの変更を有する、列挙型名スコープの伝統的な値はプログラミング中に現れる大部分のために、列挙型の安全性の問題が改善されました。
まず、伝統的な列挙型
伝統的な列挙型プログラミングプロセスで発生する可能性のある問題のいくつかを見てみましょう:
1、同じ名前の値ステートメントを持つことができないときに、2つの列挙型の宣言:
#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、列挙型定義名スコープの値:
#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) ^
二、列挙型クラス型
知られている列挙クラス、スコープの列挙には、 それが型安全かつ明示的範囲のためのものである。列挙クラスは、int型の暗黙的な変換が許可されていない、および列挙の異なるタイプの直接比較することはできません。
列挙型クラスの宣言と変数の定義:
// 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