andrewL
andrewL

Reputation:

Enum C++ Get by Index

I was wondering in C++ if I have an enum can I access the value at the second index? For example I have

enum Test{hi, bye};

if I want 'hi', can I do something like Test[0], thanks.

Upvotes: 25

Views: 95577

Answers (8)

eukariont
eukariont

Reputation: 1

#include <iostream>

#define GENERATE_ENUM(ENUM) ENUM,
#define GENERATE_STRING(STRING) #STRING,
#define FOREACH_TEST(ID) ID(hi) ID(bye) ID(good)
enum TEST { FOREACH_TEST(GENERATE_ENUM) };
static const char * Test[] = { FOREACH_TEST(GENERATE_STRING) };

int main() {

  printf("%s  ",Test[0]);  
  printf("%s\n",Test[bye]);

  for (int i=0; i<2; i++) printf("Test[%d] = %s\n", i, Test[i]); }

compile and run with: g++ enum-test.cpp -o enum-test; ./enum-test output: hi bye Test[0] = hi Test[1] = bye

Upvotes: 0

Chavan Maharshi
Chavan Maharshi

Reputation: 43

If you are excepting the value to returned as {Hi or bye} ,then you cannot get the value like that .

i would not suggest this to be done inorder to get the actual value but it can be used as hack

string return_value(int index)
{
string temp = "";
switch (index)
{
case 1: temp = "hi"
break;
case 2: temp = "bye";
break;
defualt :
break;
}
return temp;
}

typecasting to enum would again return the index but you can assign to some other enum variable

Upvotes: 0

Steve Jessop
Steve Jessop

Reputation: 279395

Depends what you mean by "I want 'hi'".

If you mean you want the value, then you can get it by casting an int, as others have said.

Casting a numeric literal to enum type is usually pointless - if you know which value you're expecting, you can use the name. That way, the enum can change without breaking your code. I guess it's possible that something really weird is going on, where someone has created an enum, and documented what "3" means but not which enum value it is. But then you'd want to fix the API.

Casting an integer value known at runtime to enum might be helpful if you have serialized data. As long as you know it's in range of the enum, the result is defined.

If you mean you want the string "hi", then you can't have it. Unlike Java, in C++ the names of the values in enumerated types exist only at compile time, not at runtime, and only map in one direction.

Upvotes: 1

JaredPar
JaredPar

Reputation: 755457

Yes and no. If your Enum does not have explicit values then it is possible. Without an explicit values, enum values are given numeric values 0-N in order of declaration. For example ...

enum Test {
  hi, // 0
  bye // 1
}

This means that indexes just translates into a literal value.

Test EnumOfIndex(int i) { return static_cast<Test>(i); }

This of course does 0 validation at runtime and as soon as you add an explicit value it will break down. But it will work in the default scenario.

Upvotes: 32

xan
xan

Reputation: 7568

Unless specified otherwise, enums start numbering at 0, and increment by 1 each entry.

enum Test
{
    hi, //0
    bye, //1
    count //2
}

You can cast an int to the type of the enum to get the value you want, such as:

(Test)0;
//or
Test(0);

Which lets you do things like:

for(int i = 0; i < count; i++)
{
    DoSomething((Test)i);
}

Upvotes: 17

An̲̳̳drew
An̲̳̳drew

Reputation: 13892

Your best option might be something like this:

enum Test{hi = 0, bye};

Then you can simply refer to 'hi' with the number 0, and 'bye' with 1.

Although this really defeats the whole purpose of using an enumeration in the first place.

Upvotes: 0

Rob Prouse
Rob Prouse

Reputation: 22657

No, but you could cast from int

Test test = (Test)0;

Upvotes: 2

strager
strager

Reputation: 90062

Enumerations map names to values. In your case, (int)hi would have a value of 0, and (int)bye a value of 1. You can use a cast to get the value of hi:

int myInteger = 0;
Test myValue = (Test)myInteger;

Note, though, that myValue could be an invalid enum value if myInteger is out of range.

Upvotes: 3

Related Questions