我知道我的回答来得太晚了,但我认为这也是一个很好的解决方案。
struct ltstr {
bool operator()(const char* s1, const char* s2) const {
return strcmp(s1, s2) < 0;
}
};
std::map<const char*, int, ltstr> msgMap;
enum MSG_NAMES{
MSG_ONE,
MSG_TWO,
MSG_THREE,
MSG_FOUR
};
void init(){
msgMap["MSG_ONE"] = MSG_ONE;
msgMap["MSG_TWO"] = MSG_TWO;
}
void processMsg(const char* msg){
std::map<const char*, int, ltstr>::iterator it = msgMap.find(msg);
if (it == msgMap.end())
return; //Or whatever... using find avoids that this message is allocated in the map even if not present...
switch((*it).second){
case MSG_ONE:
...
break:
case MSG_TWO:
...
break;
}
}