String enum
Is there a simple way to turn an enum into a string?
so I have:
1 2 3 4 5
|
enum UpdateType{
CC,
RV,
PK,
};
|
so then I have a function that accepts that enum as a parameter like this:
|
void do_something(UpdateType type);
|
and inside of that method I want to call another method like this:
|
void do_more_stuff(std::string& type);
|
I want the call to do_more_stuff to look something like this:
or like this:
|
do_more_stuff(type.to_string());
|
Is that possible? What would be the simplest way to mimic this behavior.
Lookup tables are the quick-n-easy way:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
enum UpdateType
{
CC,
RV,
PK,
};
const char* UpdateStrings[] = // or make them strings instead of const char*s
{
"CC",
"RV",
"PK"
};
//...
void do_something(UpdateType type)
{
do_more_stuff( UpdateStrings[ type ] );
}
|
Although this can maintanance tricky because you have to keep your enum and the lookup table in sync.
Topic archived. No new replies allowed.