0

Possible Duplicate:
C++: Iterate through an enum

I have following enum in c++;

typedef enum
{
    APP_NONE = 0,
    APP_SESSION = 0x80000001,
    APP_TITLE = 2,
} APP_TYPE;

I am writing a test function, which accept a string, get related integer, such as: getEnumByString("APP_NONE") = 0; or vice verse getEnumString(0)="APP_NONE".

Is it possible and how to finish it?

Community
  • 1
  • 1
David Guo
  • 1,749
  • 3
  • 20
  • 30
  • It is impossible after more investigation, since enum is just integer in runtime, no related string name information. – David Guo Jul 04 '12 at 05:16

2 Answers2

1

You can push the enum values into a container, like a vector or a set, and iterate through that.

std::vector<APP_TYPE> types;
types.push_back(APP_NONE);
types.push_back(APP_SESSION);
types.push_back(APP_TITLE);

You can use a map to associate the enum values with a string and vice versa.

std::map<APP_TYPE, std::string> type2string;
type2string[APP_NONE] = "APP_NONE";
type2string[APP_SESSION] = "APP_SESSION";
type2string[APP_TITLE] = "APP_TITLE";
jxh
  • 69,070
  • 8
  • 110
  • 193
0

Check this out: http://www.codeproject.com/Articles/42035/Enum-to-String-and-Vice-Versa-in-C

TCS
  • 5,790
  • 5
  • 54
  • 86