相当于在 不用区域的代码加上了名字。方便去寻找对应区域内的东西。
两块代码都有张三,就给两块代码分别命名为南京和上海,区分一下是南京的张三还是上海的张三。
C++测试代码如下:
//namespace 把代码的作用域加了个名字
#include <iostream>
using namespace std;
namespace spaceAA {
int g_a = 10;
}
namespace spaceBB {
struct teacher {
int id;
char name[64];
};
namespace spaceCC{
struct student {
int id;
char name[64];
};
}
}
int main()
{
using namespace spaceAA;
cout << "g_a=" << g_a << endl;
std::cout << "Hello World!\n";
using namespace spaceBB;
struct teacher t1;
t1.id = 2;
//struct student s1; //错误
using namespace spaceBB::spaceCC;
struct student s1; //正确
}