命名空间 using namespace

作用:
简化代码
代码示例:

{
using std;//在所在{}作用域中生效
cout<<"text"<<endl;
}

例2:

namespace apple{
void print(const char* text)
{
std::cout<<text<<std::endl;
}
}
int main()
{
apple::print("cherp");//::访问命名空间 调用其中的方法或函数;

namespace a=apple;//能进行赋值操作
a::print();

什么时候用:
如果项目很严肃,应将代码写在namespace后面;

posted @ 2024-07-15 07:52  Wzline  阅读(1)  评论(0编辑  收藏  举报