728x90
enum class (scoped enum)
1) 이름 공간 관리( scoped )
2) 암묵적 변환 금지
#include <iostream>
#include <vector>
#include <list>
#include <deque>
#include <map>
#include <set>
#include <algorithm>
using namespace std;
enum PlayerType
{
PT_Knight,
PT_Archer
};
enum class ObjectType
{
Player,
Monster,
ProjectTile
};
enum class ObjectType2
{
Player,
Monster,
ProjectTile
};
int main()
{
double value = PT_Knight; //가능
double value2 = ObjectType::Player;//불가능
double value3 = static_cast<double>(ObjectType::Player);//가능
int choice;
cin >> choice;
if (choice == PT_Knight)
{
}
if (choice == static_cast<int>(ObjectType::Player))
{
}
return 0;
}
일반 enum 은 멤버변수가 이름이 중복되면 안되지만 비교하기 편하고
enum class 는 이름이 중복되도 되지만 활용할때 static cast 가 필요하다
728x90
'프로그래밍 > Modern c++' 카테고리의 다른 글
override (0) | 2023.03.06 |
---|---|
delete(삭제된 함수) (0) | 2023.03.06 |
using (0) | 2023.03.06 |
nullptr (0) | 2023.03.06 |
{ } 중괄호 초기화 (0) | 2023.03.06 |
댓글