之前實現的枚舉類enum class雖然達到了當時期望的功能,但現在在用的時候出現了一些問題。比如新定義了一個Token的類,
定義如下,
class Token{public: Token(){} Token(int tp, string tx) { type = tp; text = tx; } int Type() const { return type; } string Text() const { return text; } friend ostream& operator << (ostream& out,const Token& token){ out << "<" << TokenType.Name(token.Type()) << "," << token.Text() << ">" << endl; }protected: int type; string text;};
希望Token輸出時,能夠輸出Token的類型的名字,這在之前的枚舉類裡面已經實現了,但是輸出語句中TokenType.Name(type),直接使用了TokenType對象,如果type時ExprTokenType,那麼就會出錯,除非name是static的,在TokenType和ExprTokenType裡面共用。如果這樣做,name的初始化就會很麻煩,static成員需要在類外初始化,在建構函式中不能對它進行修改,那麼,繼承的ExprTokenType也不能在建構函式裡面對它進行修改。這是一種解決方案,name在token對應的.cpp檔案裡面初始化,雖然目前只有main.cpp,其它的都是標頭檔,但是不得不增加.cpp了。這樣貌似解決了,但Token裡面輸出的那一句話還是有問題啊。
另一種方案,使用namespace,將TokenType定義為namespace,擴充的時候也不需要繼承,可以直接添加常量類型成員,而name的添加則可以放到對應Lexer的初始化裡面。
// token.hnamespace TokenType{ const static int _EOF = 0; const static int TEXT = 1; static vector<string> name = {"_EOF","TEXT"}; const string Name(int x){ return TokenType::name.at(x); }}# exprtoken.h#include "tname.h"namespace TokenType{ const static int NUMBER = 2; const static int OP = 3; static void SetNameExpr(){ TokenType::name.push_back("NUMBER"); //name.push_back("NUMBER"); TokenType::name.push_back("OP"); }}// main.cppint main(){ int type_a = TokenType::TEXT; string type_a_name = TokenType::Name(type_a); cout << "type_a:" << type_a_name << "," << type_a << endl; TokenType::SetNameExpr(); int type_b = TokenType::NUMBER; string type_b_name = TokenType::Name(type_b); cout << "type_b:" << type_b_name << "," << type_b << endl; return 0;}
本來應該很簡單的問題,為什麼現在搞得這麼複雜呢?