post
poster: Yossarian
description: enums + strings overthought
language: C++
[download]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <iostream>
#include <string>
#include <map>
using namespace std;

// Bad, enum supposed to be simple atomic type (int)!
/* enum Suit { Diamonds = "d", Hearts = "h", Clubs = "c", Spades = "s"}; 

int main(void){
    
    string input = ""; // user input    
    enum Suit currentSuit = Hearts;

    while(1){
        cout << "Guess what suit? (Diamonds, hearts, clubs, spades) by letter:\n";
        cin >> input;

        switch(input){ // BAD! 
            case Diamonds: cout << "Diamonds!\n"; break;
            case Hearts: cout << "Hearts!\n"; break;
            case Clubs: cout << "Clubs\n"; break;
            case Spades: cout << "Spades!\n"; break;
            default: "You got me hung, jack. Try again.\n"; break;   
        }
    } // While

    return 0;
} */

/* OR alternatively...
 *
 * Use preprocessor directive trap/overload extraction operator
 * but have all the enumerated types w/ strings in every symbol table.
 * Not good. :\
 *
 */


/* OR alternatively...
 *
 *   enum fruits { apples,oranges,pears };
 *   const char *fruits_str[]={ "apples","oranges","pears" };
 * 
 * //  But then you have to keep track of pointers and assert every step...
 * //  Not too bad, right? Especially if fruits is deterministic.
 */




// A clever but rather limp wristed attempt from:
// http://www.gamedev.net/topic/437852-c-enum-names-as-strings/page__view__findpost__p__3906171
class CardEnum {

        public: 
            static CardEnum enum1;
            static CardEnum enum2;
            static CardEnum enum3;

            // is this what the author originally intended?
            // what is the point if you can't stuff self-reference
            // CardEnum into map with it also being static? 
            static map<int, CardEnum> stringMap();
            int getOrdinal(){ return ordinal; }
            string getName(){ return name; }

            CardEnum(int ord, string n){
                this->ordinal = ord;
                this->name = name;
            //  stringMap[ord] = this;
            //  stringMap.insert( pair<int, CardEnum>(ord, this) );
            //  stringMap.insert( pair<int, string>(ord, this.name) );
            //  stringMap.insert(this);

            //  At this point, I question if it is worth the effort in
            //  attempting to shoehorn string into enum and just define
            //  a map/multimap with object as element - the argument
            //  being the code loses context and becomes ambiguous --
            //  What exactly is trying to be accomplished? 
            //  Is there an easier way?
            //  I think very much so!
            }

        private: 
            string name;
            int ordinal;
};


int main(void){

    CardEnum::enum1(0, "enum1"); // invoke constructor 
    CardEnum::enum1(1, "enum2");

    cout << "Testing!\n";

    return 0;
}