c++命名空间using

时间:2023-03-08 22:09:42
 #include<iostream>

 namespace run1
{
int x = ;
}
namespace run2
{
int x = ; void show()
{
std::cout << x << std::endl;
}
}
void run()
{ using namespace std;//等同局部变量
using namespace run1;
using namespace run2;//发生
//cout << "hello world"<<x;
} void main()
{
std::cout << "hello doubo";
using run2::x;
using run2::show;//单独引用命名空间变量或者函数
std::cout << x << std::endl;
show();
std::cin.get(); }
 #include<iostream>

 //using,可以省略std,
//制定命名空间,
//原则禁止using namespace std;//明确空间 namespace stdrun
{
int num = ;
void show()
{
std::cout << num << std::endl;
}
}
namespace bobo = stdrun;//给自定义的别名 namespace doubo = std;//给标准别名,不推荐
//CPP编译器设置,禁止改变标准 void main()
{
bobo::show();
doubo::cout << "hello";
doubo::cin.get();
}