Here is what I understood:
#include <cstdio> #include <string> #include <map> namespace Color { typedef enum { Red = 0, Green = 1, Blue = 2 } Color; Color colors[] = {Red, Green, Blue}; // same order as above, //to preserve index. //int colors_len = sizeof(colors)/sizeof(Color); // (if you want to check for valid values) static inline Color OfInt(int value) { // if(value >= colors_len) do error thing; return colors[value]; } } int main() { Color::Color c = Color::Red; printf("%d,", c); c = Color::OfInt(1); printf("%d,", c); c = Color::Blue; printf("%d\n", c); std::map<Color::Color, std::string> map; map[Color::Red] = "red"; return 0; }
At least he has some behavioral needs. Are you missing something you need?
It compiles with g ++ 4.3.3 and seems to work fine.
I made a namespace to translate the enumerations to another area. (so that Red is not taken, etc.) Maybe you can open it into something that you could use? :)
If you want Color :: Color outside this namespace, you can do:
typedef Color::Color ColorEnum;
But unfortunately, the name Color occupies the namespace.
source share