要從B中訪問類型定義(在A中聲明),是否需要執行「using namespace A;」
號
但是如果有一個typedef或具有相同的名稱的typedef,在命名空間B
定義的其它一些符號,那麼你需要寫:
A::some_type a;
讓我們做一個簡單的實驗來理解這一點。
考慮以下代碼:(必須閱讀評論)
namespace A
{
typedef int some_type; //some_type is int
namespace B
{
typedef char* some_type; //here some_type is char*
struct X
{
some_type m; //what is the type of m? char* or int?
A::some_type n; //what is the type of n? char* or int?
};
void f(int) { cout << "f(int)" << endl; }
void f(char*) { cout << "f(char*)" << endl; }
}
}
int main() {
A::B::X x;
A::B::f(x.m);
A::B::f(x.n);
return 0;
}
輸出:
f(char*)
f(int)
這證明的m
類型是char*
和型n
是int
如預期或預期的那樣。
在線演示:http://ideone.com/abXc8