Search code examples
c++c++11enumsenumeration

Appending a number to enum to get other enum


I want to get the enum value in a for loop, by appending a number, like

enum example
{
Example_1,
Example_2,
Example_3,
.
.
.
Example_n
};
example x;
for (i = 0; i < n; i++
{x = Example_ + i; // if i = 5, I need Example_5
}

I want this implementation in C++11


Solution

  • If you have an Example_0 in the enum, then Example_0 + 5 will give you an integer value equivalent to Example_5. Enums are just integers.

    All this assuming that you don't explicitly assign a value to a certain enumeration constant - that's another story.