[C++] Enum Class to int

빵욱·2024년 10월 16일

Enum class 는 Enum처럼 바로 int 할당은 안되고 static_cast를 사용.

#include <iostream>
using namespace std;

enum class E_OptionType {
    Red,
    Blue,
    Green,
    None
};
int main()
{
    int testNumber = static_cast<int>(E_OptionType::Blue);

    // int errorEx = E_OptionType::Blue; // Error

   cout << testNumber << "\n";

    testNumber = static_cast<int>(E_OptionType::Green);

    cout << testNumber << "\n";
}
profile
rove drink eat

0개의 댓글