2011-04-28 62 views
1

考慮以下幾點:typedef現有的枚舉類型,可能嗎?

namespace otherns 
{ 
    enum MyEnum_e { MyEnum_YES, MyEnum_NO }; 
} 

namespace myns 
{ 
    typedef otherns::MyEnum_e MyEnum_e; 
} 

爲什麼以下不是有效?

int e = myns::MyEnum_YES; 

我得到一個編譯錯誤,指出:

'MyEnum_YES' is not a member of 'myns' 

回答

4

因爲枚舉值居住在命名空間otherns,還不如MyEnum_e孩子:引用MyEnum_YES,鍵入otherns::MyEnum_YES

你可以試試這個:

namespace otherns 
{ 
    namespace MyEnum_e_space { 
    enum MyEnum_e { MyEnum_YES, MyEnum_NO }; 
    } 
    using namespace MyEnum_e_space; 
} 

namespace myns 
{ 
    using namespace otherns::MyEnum_e_space; 
} 

雖然使用using氣餒..

+2

它並不氣餒,只是用它明智;經驗法則是:不在頭文件中(因爲您不希望在頭文件的任何奇怪的未來客戶端上造成命名空間污染或符號解析衝突)。隨意在自己的編譯單元中使用'using' – sehe 2011-04-28 14:39:28