converting from/to type Enum

I have an data structure of type Enum.  I'm trying to create a function which will take a string as a parameter (the string will be one of the items in the Enum), and return the Enumerator (the number).  I've gone in circles trying to accomplish this, and I know it involves some type of casting.  The compiler (MS Visual C++ 6.0) has been trying to tell me that my goal is unrealistic.  Do I have an invalid premise here?  If not, please give me some direction.
Who is Participating?
ryanvsConnect With a Mentor Commented:
No apologies necessary, I understand your dilemma.  Depending on how dynamic the mapping structure is, you might consider loading the map from a text file or database table.  However, if you need to programmatically respond to certain values, this approach isn't quite as good.

For instance, if you are merely storing information such as auto manufacturer { Ford, Chevrolet, BMW, etc. }, this works well in a structure loaded from a database/text table.

If you need to do something special, such as credit inventory quantity when a product is returned (change from "Sold" to "Returned"), this approach does not work as well from a file/database because you need to respond to each and every value change.

Neitod's suggestion works well in this second case.

Good Luck

A little off the subject...
Neitod, I have a case where I need to translate some structures from Little Endian to Big Endian, transmit and receive, then convert from Big to Little.  I would like to build tables of the structures and their member data types, as you described, from preprocessor output but I'm not exactly sure how to do it.  The structures have a mix of characters, shorts, floats, ints, and arrays of other structs so I can't assume that I need to swap all the bytes.  Is this possible?  Thanks
You have a container of class Enum, which contains some sort of objects. You want to search the conatiner for the occurrance of a key. The search should return an object of class Enumerator.
This sounds like something for STL's map.
yes, use a map.
Never miss a deadline with

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

kpcapelAuthor Commented:
KangaRoo & nietod,

For me, this is easier said than done, but thanks for a new avenue to explore...
It doesn't have to be difficult...

typedef enum tagStringValues {  svStr1 = 1, svStr2, svStr3 } EStringValues;

typedef struct tagStringMap {
    EStringValues  value;
    char          *pszString;
} StringMap;

StringMap  astStringMap[] = {
{ svStr1, "String 1" },
{ svStr2, "String 2" },
{ svStr3, "String 3" }

#define STRINGMAPSIZE  ( sizeof(astStringMap) / sizeof(astStringMap[0]) )

EStringValues StringLookup(char *pszString)
    int i;
    for (i = 0; i < STRINGMAPSIZE; i++)
        if ( strcmp(astStringMap[i].pszString, pszString) == 0 )
            return astStringMap[i].value;
    // Unknown String (Return 0 or -1, you chose)
    return 0;

kpcapelAuthor Commented:
You're right, it doesn't have to be difficult, and I would accept your answer if not for one issue which I really wanted to avoid.    

The enum in question can and will change over time (someone has to maintain it).  I really didn't want to list the same information twice, once in the enum and once in the StringMap structure proposed.  

Sorry, there is no way to do that using built-in features of C/C++.  You need to have data structures like this.  A map would be better, but it would also need to be maintained.  What I do (as I have to do this and have 100s of these cases) is I have a pre-processor that scans my source code file looking for enum's (as well as many other things) and it "writes" a source file that contains tables and other information like this.  This other source file is then included into the main source file using an ordinary #include directive.  This is nearly automatic.  You don't have to do any of the work, but you do need to remember to run the preprocessor whenever you change things that depend on these tables.
kpcapelAuthor Commented:

Thanks for the clarification.  I guess I knew that I was reaching...


Seems I had no right to reject your answer.  My sincere appologies. Can I unreject it... anyone?
kpcapelAuthor Commented:
Thanks to ryanvs for a solution, and thanks to the others for their insight.
Somewhere in BC's code I have seen amethod where they define an enumeration in a base class. The base class also has a lookup function that returns an associated string whith each enum value. Derived classses extend the enumeration and the lookup function! This makes maintenance easier.
Can you "extend" an enumeration?  Or is this not a real "enum" enumeration.

>>  I would like to build tables of the structures and
>> their member data types, as you
>> described, from preprocessor output but I'm not
>> exactly sure how to do it.
I do that sort of thing--not for that reason.  Since parsing the C++ language is about the hardest thing in the world to do right, my preprocessor doesn't do it.  what i do is place all my structures and classes in comments using a simplified "defiition" language for the structure/class.  Then the preprocessor reads the information from the comments and generates the source code for the real c++ structure/class definitiion (places it in a .h file) and generates tables and data structures that record the class's structure and other information (these go in a seperate file that is included into the source code, not into the .h file.).
I think KangaRoo's reference is in chapter 7 of ATL Internals pub by Addison Wesley.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.