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?
enum Enum{ Banana, Orange, Apple } ; static const char * EnumStrings[] = { "bananas & monkeys", "Round and orange", "APPLE" }; const char * getTextForEnum( int enumVal ) { return EnumStrings[enumVal]; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With