enum
- enum(enumeration)
- enumerator: namespace 안에 const 선언된 값
- enum은 하나의 자료형으로 취급돼, 이를 param.로 받는 함수의 overloading 가능
enum VS enum class(struct)
- enum (= unmanaged enum = standard enum)
- enum class (= unmanaged enum = C++/CX enum)
namespace가 구분되지 않기 때문에 같은 이름의 enumerator가 존재할 수 없음.
각각 자신의 namespace가 있어 같은 이름의 enumberator를 가질 수 있음.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using std::cin; | |
using std::cout; | |
using std::endl; | |
enum PT_BASE : int {}; | |
enum PT : int { // enumeration | |
First = 1, // enumerator | |
Second, // enumerator | |
}; | |
//enum PT : int { // error: 같은 이름의 enum type ('PT')의 중복 정의 안됨. | |
// A = 11, | |
// B, | |
//}; | |
enum struct T1 : int { | |
num = 1 | |
}; | |
enum struct T2 : int { | |
num = 2 | |
}; | |
enum A1 : int { | |
a1 | |
}; | |
enum A2 : int { | |
//a1 = 2, // error: 'a1 재정의 오류' | |
// enum type이 달라도 같은 이름의 value 가질 수 없음 | |
a2 | |
}; | |
void foo(int i) { cout << i << endl; } | |
void fooEnum(PT pt) { cout << pt << endl; } | |
void foo(T1 val) { cout << "T1" << endl; } | |
void foo(T2 val) { cout << "T2" << endl; } | |
void fooA(int val) { cout << "int" << endl; } | |
void fooA(A1 val) { cout << "A1" << endl; } | |
void fooA(A2 val) { cout << "A2" << endl; } | |
int main() | |
{ | |
// case1: enum:int는 int로 간주됨 | |
cout << endl << "Case 1" << endl; | |
cout << PT::First << endl; // output: 1 | |
cout << PT::Second<< endl; // output: 2 | |
// case2: enum type을 인수로 받는 함수에 int를 넣을 시 에러. | |
cout << endl << "Case 2" << endl; | |
foo(PT::First); // output: 1 | |
//fooEnum(1); // error 'int'형식이 'PT'형식과 호환되지 않습니다.' | |
// case3: enum struct test | |
cout << endl << "Case 3" << endl; | |
//foo(T1); // error '형식 이름을 사용할 수 없습니다.' | |
foo(T1::num); // output: T1 | |
foo(T2::num); // output: T2 | |
// case4: enum test. | |
cout << endl << "Case 4" << endl; | |
foo(A1::a1); // output: 0 | |
fooA(A1::a1); // output: A1 | |
fooA(A2::a2); // output: A2 | |
//fooA(1); // error '인수 목록이 일치하는 오버로드된 fooA가 없습니다.' | |
return 0; | |
} |
댓글 없음:
댓글 쓰기