logo

C++의 Enum 클래스와 Enum DataType에 비해 장점

열거형 또는 열거형(열거형) 일부 제한된 값을 할당할 수 있는 사용자 정의 데이터 유형입니다. 이 값은 열거형을 선언할 때 프로그래머에 의해 정의됩니다.

Enum 유형보다 Enum 클래스가 필요합니다.
다음은 Enum Type의 제한 사항과 이를 처리하기 위해 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;> }>

>

>

문자열을 json으로 변환 java
산출

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 클래스라고도 함)가 도입되었습니다. 범위가 지정된 열거 ), 이는 열거형을 둘 다로 만듭니다. 강력한 유형 및 강력한 범위 . 클래스 열거형은 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;> }>

>

>

산출

우분투의 ipconfig
It's not Red Good people 1>

열거형 클래스로 선언된 열거형은 기본 유형에 대해 더 많은 제어권을 갖습니다. 이는 기본적으로 유형의 크기를 결정하는 역할을 하는 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:'<

>

>

산출

size of enum rainbow variable: 4 size of enum class eyecolor variable:1>

참조: https://en.cppreference.com/w/cpp/언어/enum