How to cast int to enum in C++?

user1509260 picture user1509260 · Jul 12, 2012 · Viewed 280k times · Source

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?

Answer

Andrew picture Andrew · Jul 12, 2012
int i = 1;
Test val = static_cast<Test>(i);