如何在c++中将int转换为enum ?

时间:2022-03-02 16:00:22

Possible Duplicate:
Generic way to cast int to enum in C++

可能的重复:在c++中将int转换为enum的通用方式

How do I cast an int to an enum in C++?

如何在c++中对枚举进行int转换?

For example:

例如:

enum Test
{
    A, B
};

int a = 1;

How do I convert a to type Test::A?

如何将a转换为类型测试::a ?

3 个解决方案

#1


159  

int i = 1;
Test val = static_cast<Test>(i);

#2


52  

Test e = static_cast<Test>(1);

#3


14  

Your code

你的代码

enum Test
{
    A, B
}

int a = 1;

Solution

解决方案

Test castEnum = (Test)a;

#1


159  

int i = 1;
Test val = static_cast<Test>(i);

#2


52  

Test e = static_cast<Test>(1);

#3


14  

Your code

你的代码

enum Test
{
    A, B
}

int a = 1;

Solution

解决方案

Test castEnum = (Test)a;