命名空间是为了区分同名变量或者函数的。创建的时候名字不能重复。
三种使用方法:
<1> using namespace name;
<2> name::variable name/function name (::作用域运算符)
<3> using name::member(指定开放某个成员)
代码:
#include <iostream>
using namespace std;
namespace spaceone {
void putMsg() {
cout << "spaceone msg."<<endl;
}
}
namespace spacetwo {
void putMsg() {
cout << "spacetwo msg." << endl;
}
}
using namespace spaceone;
int main()
{
putMsg();
spacetwo::putMsg();
return 0;
}
打印结果:
spaceone msg.
spacetwo msg.