列挙型または列挙型 (列挙型) は、いくつかの制限された値を割り当てることができるユーザー定義のデータ型です。これらの値は、列挙型の宣言時にプログラマによって定義されます。
Enum 型ではなく Enum クラスが必要:
以下に、Enum 型の制限とは何なのか、またそれらをカバーするために Enum クラスが必要な理由のいくつかを示します。
1.Enum は名前付き整数定数のコレクションであり、各要素が整数値によって割り当てられることを意味します。 2.enumキーワードで宣言します。
C++
#include> using> namespace> std;> enum> roll_no {> >satya = 70,> >aakanskah = 73,> >sanket = 31,> >aniket = 05,> >avinash = 68,> >shreya = 47,> >nikita = 69,> };> int> main()> {> >enum> roll_no obj;> >obj = avinash;> >cout <<>'The roll no of avinash='> << obj;> }> |
>
>
Pythonのパス設定出力
The roll no of avinash=68>
二 列挙 同じ名前を共有することはできません:
CPP
ユーザー名の例
#include> 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; ^> すでに列挙型に含まれている名前を変数に付けることはできません。
CPP
#include> 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, ^> 列挙型はそうではありません タイプセーフ :
CPP
#include> 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) ^>
列挙型クラス
C++11 では enum クラス (とも呼ばれます) が導入されました。 スコープ付き列挙 )、列挙は両方になります 厳密に型指定され、厳密にスコープ指定されている 。クラス enum では int への暗黙的な変換が許可されておらず、また、異なる列挙型の列挙子を比較することもできません。
enum クラスを定義するには、enum キーワードの後に class キーワードを使用します。
構文:
// Declaration enum class EnumName{ Value1, Value2, ... ValueN}; // Initialisation EnumName ObjectName = EnumName::Value;> 例:
// Declaration enum class Color{ Red, Green, Blue}; // Initialisation Color col = Color::Red;> 以下はEnumクラスを表示するための実装です。
CPP
// C++ program to demonstrate working> // of Enum Classes> #include> 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
'>;> >else> >cout <<>'It's not Red
'>;> >People p = People::Good;> >if> (p == People::Bad)> >cout <<>'Bad people
'>;> >else> >cout <<>'Good people
'>;> >// 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>
enum クラスで宣言された列挙型も、その基になる型をより詳細に制御できます。 char、short、unsigned int などの整数データ型であれば、基本的に型のサイズを決定するのに役立ちます。
これは、列挙型に続くコロンと基になる型によって指定されます。
eg: enum class eyecolor : char {char,green,blue}; Here eyecolor is a distinct type with the same size as a char (1 byte).> C++
#include> using> namespace> std;> enum> rainbow{> >violet,> >indigo,> >blue,> >green,yellow,orange,red> }colors;> enum> class> eyecolor:>char>{> >blue,green,brown> }eye;> int> main() {> >cout<<>'size of enum rainbow variable: '><<>sizeof>(colors)< cout<<'size of enum class eyecolor variable:'< |
>
>出力
PVR 完全形式
size of enum rainbow variable: 4 size of enum class eyecolor variable:1>
参照: https://en.cppreference.com/w/cpp/ language/enum