补充一个命名空间冲突的情况:
#include
usingnamespace std;
namespace A {
int a =100;
namespace B //嵌套一个命名空间B
{
int a =20;
}
}
int a =200;//定义一个全局变量
int main(int argc,char*argv[]){
cout <<"A::a ="<< A::a << endl; //A::a =100
cout <<"A::B::a ="<< A::B::a << endl; //A::B::a =20
cout <<"a ="<< a << endl; //a =200
cout <<"::a ="<<::a << endl; //::a =200
usingnamespace A;
cout <<"a ="<< a << endl; // Reference to 'a' is ambiguous // 命名空间冲突,编译期错误
cout <<"::a ="<<::a << endl;//::a =200
int a =30;
cout <<"a ="<< a << endl; //a =30
cout <<"::a ="<<::a << endl;//::a =200
//即:全局变量 a 表达为 ::a,用于当有同名的局部变量时来区别两者。
usingnamespace A;
cout <<"a ="<< a << endl; // a =30 // 当有本地同名变量后,优先使用本地,冲突解除
cout <<"::a ="<<::a << endl;//::a =200
return0;
}