enum class的基於namespace的實現

來源:互聯網
上載者:User

之前實現的枚舉類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;}

本來應該很簡單的問題,為什麼現在搞得這麼複雜呢?

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.