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.

1
  • I should point that using strings as enums is probably a poor design choice. Commented Feb 3, 2009 at 19:58

3 Answers 3

2

I reviewed this approach awhile ago - available via Code Project

Sign up to request clarification or add additional context in comments.

Comments

0

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

1 Comment

or boost/TR1 unordered_map (instead of the non-standard hash_map)
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 ) );

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.