Blog Details

C++命名空间 namespace的详细讲解

命名空间

命名空间 namespace

1、::作用域运算符(表明 数据、方法 的归属性问题)

2、命名空间 namespace 解决命名冲突

2.1:namespace命名空间的定义

2.2:命名空间只能全局范围内定义(以下错误写法)

2.3:命名空间可嵌套命名空间

2.4:命名空间是开放的,即可以随时把新的成员加入已有的命名空间中(常用)

2.5:命名空间 可以存放 变量 和 函数

2.6:命名空间中的函数 可以在“命名空间”外 定义

2.7:无名命名空间,意味着命名空间中的标识符只能在本文件内访问,相当于给这个标识符加上了static,使得其可以作为内部连接

2.8:给命名空间 取个别名

3. using 使用命名空间

3.1、简化了从命名空间的成员访问

3.2、using 使用整个命名空间

3.3、using 指明使用具体的命名空间 成员。(了解就可以)

3.4、using声明碰到函数重载(了解)

3.5、不同命名空间中的 同名成员 使用的时候注意 二义性

总结:

命名空间 namespace

1、::作用域运算符(表明 数据、方法 的归属性问题)

using namespace std;

int a = 10;//全局变量

void test01()

{

int a = 20;//局部变量

cout<<"局部变量a = "<

//::作用域运算符(c++独有)

cout<<"全局变量a = "<<::a<

}

2、命名空间 namespace 解决命名冲突

2.1:namespace命名空间的定义

namespace 可以自己修改或创建

//定义一个名字为A的命名空间(变量、函数)

namespace A {

int a = 100;

}

namespace B {

int a = 200;

}

void test02()

{

//A::a a是属于A中

cout<<"A中a = "<

cout<<"B中a = "<

}

2.2:命名空间只能全局范围内定义(以下错误写法)

2.3:命名空间可嵌套命名空间

namespace A {

int a = 1000;

namespace B {

int a = 2000;

}

}

void test03()

{

cout<<"A中的a = "<

cout<<"B中的a = "<

}

2.4:命名空间是开放的,即可以随时把新的成员加入已有的命名空间中(常用)

namespace A {

int a = 100;

int b = 200;

}

//将c添加到已有的命名空间A中

namespace A {

int c = 300;

}

void test04()

{

cout<<"A中a = "<

cout<<"A中c = "<

}

2.5:命名空间 可以存放 变量 和 函数

namespace A {

int a=100;//变量

void func()//函数

{

cout<<"func遍历a = "<

}

}

void test05()

{

//变量的使用

cout<<"A中的a = "<

//函数的使用

A::func();

}

2.6:命名空间中的函数 可以在“命名空间”外 定义

namespace A {

int a=100;//变量

void func();

}

void A::func()//成员函数 在外部定义的时候 记得加作用域

{

//访问命名空间的数据不用加作用域

cout<<"func遍历a = "<

}

void funb()//普通函数

{

cout<<"funb遍历a = "<

}

void test06()

{

A::func();

funb();

}

运行结果:

2.7:无名命名空间,意味着命名空间中的标识符只能在本文件内访问,相当于给这个标识符加上了static,使得其可以作为内部连接

namespace{

int a =10;

void func(){

cout << "hello namespace "<< endl}

}

void test(){

cout << "a:" << a << endl;

func();

}

2.8:给命名空间 取个别名

namespace veryLongName{

int a = 10;

void func(){

cout << "hello namespace" << endl; }

}

void test(){

namespace shortName = veryLongName;

cout << "veryLongName::a : " << shortName::a << endl;

veryLongName::func();

shortName::func();

}

3. using 使用命名空间

3.1、简化了从命名空间的成员访问

namespace veryLongName {

int a=100;

void func(){

cout<<"hello namespace"<

}

void test07()

{

//使用veryLongName命名空间

using namespace veryLongName;

//出现的变量 从veryLongName命名空间中找 找不到 从其他地方中

cout<<"a = "<

func();

}

3.2、using 使用整个命名空间

namespace veryLongName {

int a=100;

void func(){

cout<<"hello namespace"<

}

void test07()

{

int a=200;

//使用veryLongName命名空间

using namespace veryLongName;

//出现的变量 从veryLongName命名空间中找 找不到 从其他地方中

cout<<"a = "<

cout<<"a = "<

func();

}

3.3、using 指明使用具体的命名空间 成员。(了解就可以)

using直接使用 命名空间中的成员 会和 局部变量冲突

namespace veryLongName {

int a = 100;

void func(){

cout << "hello namespace" << endl;}

}

void test01()

{

//using直接使用 命名空间的成员会和局部变量冲突

int a = 200;

//指明 使用命名空间中的具体成员 容易和其他变量冲突

using veryLongName::a;//err

cout << "a = " << a << endl;

//但是func使用的时候 必须加作用域

veryLongName::func();

}

using直接使用 命名空间中的成员 不会和 全局变量冲突

namespace veryLongName {

int a=100;

void func(){

cout<<"hello namespace"<

}

int a = 200;

void test07()

{

//using直接使用 命名空间中的成员 不会和 全局变量冲突

using veryLongName::a;

cout<<"命名空间中a = "<

cout<<"全局变量中a = "<<::a<

//但是func使用的时候 必须加作用域

veryLongName::func();

}

3.4、using声明碰到函数重载(了解)

namespace A {

//函数重载 函数名+参数 组合代表是函数的入口地址

void func(){

cout<<" 无参的func"<

void func(int a){

cout<<" int的func"<

void func(int a,int b){

cout<<" int int的func"<

}

void test08()

{

//using指明 使用 A中的func 会对 所有的func起作用

using A::func;

func();

func(10);

func(10,20);

}

运行结果:

3.5、不同命名空间中的 同名成员 使用的时候注意 二义性

namespace A {

int a = 10;

}

namespace B {

int a = 20;

}

void test09()

{

//此处的a 不知道是A还是B中a

//cout<<"a = "<

//解决方法

cout<<"A::a = "<

cout<<"B::a = "<

}

总结:

namespace A

{

int a = 10;

void func(){

cout<<"func"<

}

1、命名空间的定义( 不能在 函数内 定义命名空间) 2、使用命名空间的成员 最安全的方式 命名空间名::成员名 3、using namespace 命名空间名;使用整个命名空间 (重要)

using namespace A;

4、单独 使用命名空间中的具体成员:using 命名空间名::成员名;

using A::a;

5、说明一下main中的std

#include

//使用标准的命名空间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;

}