I commonly find I need to convert an enum to a string in c++
I always end up doing:
enum Enum{ Banana, Orange, Apple } ;
char * getTextForEnum( int enumVal )
{
switch( enumVal )
{
case Enum::Banana:
return "bananas & monkeys";
case Enum::Orange:
return "Round and orange";
case Enum::Apple:
return "APPLE" ;
default:
return "Not recognized..";
}
}
Is there a better or recognized idiom for doing this?
std::map<int, std::string>instead of enum and switch? With a bit of wrapping into an object you could make it smart enough to be reusable as an error code lookup and handler for all your apps.