How to cast int to enum in C++?

238

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

For example:

enum Test
{
    A, B
};

int a = 1;

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

This question is tagged with c++ casting enums

~ Asked on 2012-07-12 13:30:36

The Best Answer is


266

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

~ Answered on 2012-07-12 13:33:14


76

Test e = static_cast<Test>(1);

~ Answered on 2012-07-12 13:33:15


Most Viewed Questions: