在同一空间,有相同的命名,访问会发生冲突,相同的命名空间定义在不同的命名空间中,就可以避免冲突,灵活使用。
//命名空间--相同的命名,定义在不同的空间,就可以无冲突的访问
#include<iostream>
using namespace std;//这里定义了下面程序 cout,cin endl,的空间
namespace nsConst
{
const int Top=1;
const int Bottom=2;
const int Left=3;
const int Right=4;
}
namespace nsMemo
{
const char *Top="上";
const char *Bottom="下";
const char *Left="左";
const char *Right="右";
}
int main()
{
int n;
cin>>n;
switch (n)
{
case nsConst::Left:
cout<<nsMemo::Left<<endl;
break;
case nsConst::Top:
cout<<nsMemo::Top<<endl;
break;
case nsConst::Right:
cout<<nsMemo::Right<<endl;
break;
case nsConst::Bottom:
cout<<nsMemo::Bottom<<endl;
break;
default:
break;
}
system("pause");
return 0;
}
在nsConst和nsMemo里都有Bottom,但是相同命名的值不一样;