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

命名空间 namespace

在这里插入图片描述

人生重要的不是所站的位置,而是所朝的方向;

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

using namespace std;
int a = 10;//全局变量
void test01()
{
    int a = 20;//局部变量
    cout<<"局部变量a = "<<a<<endl;//优先选择局部变量
    //::作用域运算符(c++独有)
    cout<<"全局变量a = "<<::a<<endl;//取全局变量
}

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 = "<<A::a<<endl;//100
    cout<<"B中a = "<<B::a<<endl;//200
}

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

在这里插入图片描述

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

namespace A {
    int a = 1000;
    namespace B {
        int a = 2000;
    }
}
void test03()
{
    cout<<"A中的a = "<<A::a<<endl; //1000
    cout<<"B中的a = "<<A::B::a<<endl; //2000
}

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

namespace A {
    int a = 100;
    int b = 200;
}
//将c添加到已有的命名空间A中
namespace A {
    int c = 300;
}
void test04()
{
    cout<<"A中a = "<<A::a<<endl;//100
    cout<<"A中c = "<<A::c<<endl;//200
}

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

namespace A {
    int a=100;//变量

    void func()//函数
    {
        cout<<"func遍历a = "<<a<<endl;
    }
}
void test05()
{
    //变量的使用
    cout<<"A中的a = "<<A::a<<endl;

    //函数的使用
    A::func();
}

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

namespace A {
    int a=100;//变量

    void func();
}

void A::func()//成员函数 在外部定义的时候 记得加作用域
{
    //访问命名空间的数据不用加作用域
    cout<<"func遍历a = "<<a<<endl;
}

void funb()//普通函数
{
    cout<<"funb遍历a = "<<A::a<<endl;
}
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"<<endl;}
}
void test07()
{

    //使用veryLongName命名空间
    using namespace veryLongName;

    //出现的变量 从veryLongName命名空间中找 找不到 从其他地方中
    cout<<"a = "<<a<<endl;
    func();
}

3.2、using 使用整个命名空间

namespace veryLongName {
    int a=100;
    void func(){cout<<"hello namespace"<<endl;}
}
void test07()
{
    int a=200;
    //使用veryLongName命名空间
    using namespace veryLongName;

    //出现的变量 从veryLongName命名空间中找 找不到 从其他地方中
    cout<<"a = "<<a<<endl;//访问的是局部变量中的a
    cout<<"a = "<<veryLongName::a<<endl;//访问的是veryLongName的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"<<endl;}
}
int a = 200;
void test07()
{
    //using直接使用 命名空间中的成员 不会和 全局变量冲突
    using veryLongName::a;

    cout<<"命名空间中a = "<<a<<endl;//命名空间中的成员 100
    cout<<"全局变量中a = "<<::a<<endl;//200

    //但是func使用的时候 必须加作用域
    veryLongName::func();
}

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

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 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 = "<<a<<endl;//err

    //解决方法
    cout<<"A::a = "<<A::a<<endl;//100
    cout<<"B::a = "<<B::a<<endl;//200
}

总结:

namespace A
{
    int a = 10;
    void func(){cout<<"func"<<endl;}
}

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

using namespace A;

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

using A::a;

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;
}
  • 48
    点赞
  • 164
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
命名空间是一种用于组织代码和避免命名冲突的机制。它可以将全局作用域划分为多个独立的区域,每个区域中的标识符(如变量、函数、类等)都具有唯一的名称。 通过使用命名空间,可以将相关的代码组织到一起,提高代码的可读性和可维护性。命名空间可以包含变量、函数、类、结构体和枚举等,这些元素都属于该命名空间的成员。 在 C++ 中,可以通过关键字`namespace`来定义一个命名空间。例如: ```cpp namespace MyNamespace { int myVariable; void myFunction(); class MyClass { // 类的定义 }; } ``` 在上面的例子中,定义了一个名为`MyNamespace`的命名空间,它包含一个整型变量`myVariable`、一个函数`myFunction()`和一个类`MyClass`。 要在代码中使用命名空间中的成员,可以通过作用域解析操作符`::`来引用,例如: ```cpp int main() { MyNamespace::myVariable = 42; MyNamespace::myFunction(); MyNamespace::MyClass obj; // 其他代码 return 0; } ``` 通过使用命名空间,可以避免不同模块之间的命名冲突,不同命名空间中的相同名称的标识符不会发生冲突。此外,命名空间还可以嵌套定义,以创建更复杂的层次结构。 总结来说,命名空间是一种用于组织代码和避免命名冲突的机制,它将全局作用域划分为不同的区域,每个区域中的标识符具有唯一的名称。使用命名空间可以提高代码的可读性和可维护性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值