1、::作用域运算符(表明数据、方法的归属性问题)
示例:
#inclued <iostream>
using namespace std;
int a = 100;
int main(int argc,char argv[])
{
int a = 200;
cout << "局部的a变量 = " << a << endl;//优先选择局部变量 结果为100
//::作用域运算符 C++独有
cout << "全局的a变量 = " << ::a << endl;//取全局变量 结果为200
}
2、 命名空间 namespace 解决命名冲突问题
namespace 命名空间的定义
namespace A{ //定义一个名为A的命名空间
int a = 100;
}
namespace B{
int a = 200;
}
void test1()
{
/* namespace A{ //namespace只能在全局定义 不可以在局部定义
int a = 100;
}
namespace B{
int a = 200;
} */
cout << "A中a = " << A::a << endl;//结果为100 A::a表示a是属于A中的
cout << "B中a = " << B::a << endl;//结果为200 B::a表示a是属于B中的
}
命名空间嵌套命名空间
namespace A{
int a = 1000;
namespace B{
int a = 2000;
}
}
void test2()
{
cout << "A中a = " << A::a << endl;//1000
cout << "B中a = " << A::B::a << endl;//2000
}
命名空间是开放的
命名空间为开放的 可以随时将新成员加入已有命名空间
namespace A{
int a = 100;
int b = 200;
}
//将c加入命名空间A 中
namespace A{
int c = 300;
}
void test3()
{
cout << "A中c = " << A::c << endl;
}
命名空间可以存放变量 函数
namespace A {
int a=100;//变量
void func()//函数
{
cout<<"func遍历a = "<<a<<endl;
}
}
void test4()
{
cout<<"A中的a = "<<A::a<<endl;//变量的使用
A::func();//函数的使用
命名空间的函数可以在命名空间外定义
namespace A {
int a=100;//变量
void func();
}
void A::func()//成员函数在外部定义的时候要加作用域
{
cout << "func遍历a = " << a << endl;//访问命名空间的数据不用加作用域
}
void test5()
{
A::fun();//func遍历a = 100
}
无名命名空间
无名命名空间意味着空间中的标识符只能在本文件内访问,相当于给这个标识符加上了static,使得其可以作为内部连接
给命名空间取个别名
namespace veryLongName{
int a = 10;
void func(){ cout << "hello namespace" << endl; }
}
void test6(){
namespace shortName = veryLongName;
cout << "veryLongName::a : " << shortName::a << endl;
veryLongName::func();
shortName::func();
}
3、using使用命名空间
简化了从命名空间的成员访问
using 使用整个命名空间
namespace veryLongName {
int a = 100;
int b = 10;
void func()
{
cout << "hello namespace" << endl;
}
}
void test7()
{
int b = 20;
//使用veryLongName命名空间
using namespace veryLongName;
//出现的变量 从veryLongName命名空间中找 找不到 从其他地方中
cout << "a = " << a << endl;//访问的是veryLongName中的a
cout << "b = " << b << endl;//访问的是局部变量中的b
cout << "b = " << veryLongName::b << endl;//访问的是veryLongName的b
func();
}
using 指明使用具体的命名空间 成员。
using直接使用 命名空间中的成员会和局部变量冲突
namespace veryLongName {
int a = 100;
void func()
{
cout << "hello namespace" << endl;
}
}
void test8()
{
int a = 200;
using veryLongName :: a;//err
cout << "a = " << a << endl;
veryLongName :: func();
}
using直接使用 命名空间中的成员不会和全局变量冲突
namespace veryLongName {
int a=100;
void func()
{
cout << "hello namespace" << endl;
}
}
int a = 200;
void test9()
{
//using直接使用 命名空间中的成员 不会和 全局变量冲突
using veryLongName::a;
cout<<"命名空间中a = "<<a<<endl;//命名空间中的成员 100
cout<<"全局变量中a = "<<::a<<endl;//200
//但是func使用的时候 必须加作用域
veryLongName::func();
}
3、函数重载
namespace A {
//函数重载 函数名+参数 组合代表是函数的入口地址
void func(){cout<<" 无参的func"<<endl;}
void func(int a){cout<<" int的func"<<endl;}
void func(int a,int b){cout<<" int int的func"<<endl;}
}
void test10()
{
//using指明 使用 A中的func 会对 所有的func起作用
using A::func;
func();
func(10);
func(10,20);
}
总结
1、命名空间的定义( 不能在 函数内 定义命名空间);
2、使用命名空间的成员 最安全的方式 命名空间名::成员名;
3、using namespace 命名空间名;使用整个命名空间;
4、单独 使用命名空间中的具体成员:using 命名空间名::成员名;
5、main中的std:
#include <iostream>
//使用标准的命名空间std
//std中所有成员名 可以直接使用
//cout endl cin都是命名空间std的成员
using namespace std;
int main(int argc, char *argv[])
{
std::cout << "Hello World!" << std::endl;
cout << "Hello World!" << endl;
return 0;
}