1

Strings to enum in C#, how do you normally converting strings to enum in C++. Any helper function that you use, is it a good idea to do this.

Community
  • 1
  • 1
kal
  • 28,545
  • 49
  • 129
  • 149

3 Answers3

2

I reviewed this approach awhile ago - available via Code Project

SAMills
  • 446
  • 4
  • 13
0
#include <EnumString.h>

from http://codeproject.com/Articles/42035/Enum-to-String-and-Vice-Versa-in-C and after

enum FORM {
    F_NONE = 0,
    F_BOX,
    F_CUBE,
    F_SPHERE,
};

insert

Begin_Enum_String( FORM )
{
    Enum_String( F_NONE );
    Enum_String( F_BOX );
    Enum_String( F_CUBE );
    Enum_String( F_SPHERE );
}
End_Enum_String;

Work fine, if values in enum are not dublicate.

Example in code

enum FORM f = ...
const std::string& str = EnumString< FORM >::From( f );

and vice versa

assert( EnumString< FORM >::To( f, str ) );
0

You will probably need to use a std::map or hash_map data structure.

Crashworks
  • 40,496
  • 12
  • 101
  • 170