#include <iostream>using namespace std;namespace verylongName { int a = 100; void func(){ cout<<"hello"<<endl; }} void test(){ //给命名空间起别名 namespace A = verylongName; cout<<"verylongName::a"<<A::a<<endl; verylongName::func(); A::func();}int main(int argc, char *argv[]){ test(); return 0;}
输出结果:
知识点3【using使用命名空间】
1、简化了从命名空间的访问:
#include <iostream>using namespace std; namespace verylongName { int a = 100; void func(){ cout<<"hello namespace"<<endl; }} void test(){ //这种方式最安全(加作用域) cout<<"verylongName::a = "<<verylongName::a<<endl; verylongName::func(); //从当前位置使用verylongName命名空间 using namespace verylongName; //以后出现的变量,先从verylongName命名空间中找, //找不到,再从其他地方找(局部,全局),如果还没有,则报错 cout<<"a = "<<a<<endl; func();}int main(){ test(); return 0;}
2、代价(容易造成冲突)
#include <iostream>using namespace std; namespace verylongName { int a = 100; void func(){ cout<<"hello namespace"<<endl; }}void test(){ int a = 900; //这种方式最安全(加作用域) cout<<"verylongName::a = "<<verylongName::a<<endl; verylongName::func(); //从当前位置使用verylongName命名空间 using namespace verylongName; //以后出现的变量,先从verylongName命名空间中找, //找不到,再从其他地方找(局部,全局),如果还没有,则报错 cout<<"a = "<<a<<endl;//冲突,就近原则,访问的是局部变量中的a func(); //此时访问时需要回归本质 cout<<"a = "<<verylongName::a<<endl;//访问的是命名空间内的 func();}int main(){ test(); return 0;}
运行结果:
3、using指明使用具体的命名空间成员
直接使用命名空间中的成员会和局部变量冲突,但不会和全局变量冲突(含有隐藏的作用域 ::),会优先选择命名空间中的成员变量,如果想要访问全局变量 ,则加作用域 :: 。
#include <iostream>using namespace std; namespace verylongName { int a = 100; void func(){ cout<<"hello namespace"<<endl; }}int a = 800;//全局变量不会产生冲突void test(){ //int a = 900;//局部变量没有任何符号可以去修饰 //指明使用命名空间中的具体成员 using verylongName::a;//当此时有同名局部变量时,会出错; //为全局变量时会打印指定的具体成员变量 cout<<"a = "<<a<<endl;//100 cout<<"a = "<<::a<<endl;//800 //但是func使用的时候必须加作用域 //func();//出错 verylongName::func();}int main(){ test(); return 0;}
最安全的访问方式:加作用域。
4、using重载函数(了解)
C语言中,函数名代表函数的入口地址;
C++中,函数名+参数,组合才代表函数的入口地址。
无命名空间时:
#include <iostream>using namespace std; void func(){cout<<"无参的func"<<endl;}//func_void 内部转换void func(int a){cout<<"int的func"<<endl;}//func_intvoid func(int a, int b){cout<<"int int的func"<<endl;}//func_int_int int main(){ func(); //无参的func func(10); //int的func func(20,30); //int int的func return 0;}
有命名空间:
函数重载时,using指明使用A中的func,会对所有的func起作用:
#include <iostream>using namespace std; namespace A{void func(){cout<<"无参的func"<<endl;}//func_void 内部转换void func(int a){cout<<"int的func"<<endl;}//func_intvoid func(int a, int b){cout<<"int int的func"<<endl;}//func_int_int} int main(){ //using指明使用A中的func,会对所有的func起作用: using A::func; func(); //无参的func func(10); //int的func func(20,30); //int int的func#if 0 A::func(); //无参的func A::func(10); //int的func A::func(20,30); //int int的func#endif return 0;}
5、不同命名空间中的同名成员,使用时注意二义性
解决方法:添加作用域即可
命名空间总结:
namespace A{ int a = 10; void func() { cout<<"func"<<endl; }}
1、命名空间的定义:不能在函数内定义命名空间
2、使用命名空间的成员最安全的方式:
命名空间名::成员名
3、using namespace 命名空间名,表示使用整个命名空间(重点)
using namespace A;
4、单独使用命名空间中的具体成员:
using 命名空间名::成员名;
using A::a;
标准命名空间:
using namespace std;//std中所有成员名,可以直接使用
当没有写标准命名空间时,使用cout等命令时会出现错误;
解决方法:
#include<iostream>//using namespace std;int main(){ std::cout<<"hello"<<std::endl; returnn 0;}
本文地址:百科问答频道 https://www.neebe.cn/wenda/918238_2.html,易企推百科一个免费的知识分享平台,本站部分文章来网络分享,本着互联网分享的精神,如有涉及到您的权益,请联系我们删除,谢谢!