初识C++之命名空间(namespace)

初识C++之入门 命名空间(namespace)

1.为什么要有命名空间

  在C/C++中,变量和函数如果大量存在,变量和函数都存放在全局作用域中,可能会导致命名冲突。其次,在一个多人合作的项目中,也可能出现相同的变量名,这就是为什么要有命名空间,使用命名空间的目的是对标识符的名称进行本地化,以避免命名冲突或名字污染

例如:

#define _CRT_SECURE_NO_WARNINGS 1

#include <stdio.h>
#include <stdlib.h>

int rand = 0;

int main()
{
	printf("%p\n", rand);
	return 0;
}

运行结果
在这里插入图片描述

解释;
在C++中rand是一个函数,其实再定义一个rand变量时,就会出现命名冲突,在C语言中,没有办法解决,所以在C++中提出了命名空间 namespace解决

2. 命名空间 namespace使用方法

  在使用命名空间 namespace之前,先来介绍一下在编译默认的查找顺

  1. 当前局部作用域 (也就是在同一个花括号)
  2. 全局作用域 (也就是不被花括号 括起来的)
  3. 如果有展开命名空间则去命名空间找(并不是存在命名空间就会去找)

了解了以上就可以开始使用 namespace

3. 作用域限定符(::)和 命名空间(namespace)

  1. 可以通过作用域限定符(::)来访问全局变量

例1:

#include <iostream>

int a = 20;

int main()
{
	int a = 10;
	printf("%d\n", a);   //访问局部变量的a
	printf("%d\n", ::a); //访问全局变量的a
	return 0;
}

代码运行结果:
10
20

解释:
在不同作用域中可以存在相同名称的变量,当时在局部作用域中访问相同名称的变量时,遵循局部优先原则,会优先访问局部变量,但是在前面加上作用域限定符(::)即可访问全局变量

4. 命名空间的定义

可以通过作用域限定符(::)来访问命名空间(namespace)中的变量

例2:

#include <iostream>

namespace test 
{
	int rand = 10;

	int Add(int x, int y)
	{
		return x + y;
	}

	struct student
	{
		char name[10];
		int age;
		float score;
	};
}

int main()
{
	printf("%p\n", rand);   //由于在C++中 rand 是一个库函数,所以使用%p打印地址
	printf("%d\n", test::rand); //在作用域限定符(::)前加上命名空间名则可访问
	printf("%d\n", test::Add(1, 3));

	return 0;
}

解释:
test是命名空间名
在命名空间中可以定义变量,函数以及类型

TIPS:如果在同一个工程文件中存在相同名称的命名空间,在编译时则会合并和一个命名空间

5. 命名空间的嵌套

如果出现命名空间名也重复的情况下,则可以嵌套命名空间
例3:

#include <iostream>

namespace Lin
{
	namespace test
	{
		int rand = 10;

		int Add(int x, int y)
		{
			return x + y;
		}

		struct student
		{
			char name[10];
			int age;
			float score;
		};
	}

}

int main()
{
	printf("%p\n", rand);
	printf("%d\n", Lin::test::rand);
	printf("%d\n", Lin::test::Add(1, 3));

	return 0;
}

6. 命名空间的使用

1. 通过命名空间名加域作用限定符

#include <iostream>

namespace test
{
	int a = 20;
}

int main()
{
	printf("%d\n",test::a);

	return 0;
}

代码运行结果:
20

2. 使用using展开部分命名空间

#include <iostream>

namespace test
{
	int a = 20;
	int b = 30;
}

using test::b;

int main()
{
	printf("a = %d\n",test::a);
	printf("b = %d\n",b);

	return 0;
}

代码运行结果:
a = 20
b = 30

3. 使用using展开全部命名空间

#include <iostream>

namespace test
{
	int a = 20;
	int b = 30;
}

using namespace test;

int main()
{
	printf("a = %d\n", test::a);
	printf("b = %d\n", b);

	return 0;
}

7. 总结

1. 在编译时,会优先查找局部作用域,其次是全局作用域,如果存在展开的命名空间,也会去命名空间找 2. 命名空间中可以定义变量,函数以及类型 3. 命名空间可以使用命名空间名 + 作用域限定符使用,也可以通过using先展开命名空间的部分或全部使用

相关推荐

  1. namespace命名空间

    2024-03-29 09:02:07       15 阅读
  2. c++基础认识

    2024-03-29 09:02:07       25 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-03-29 09:02:07       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-29 09:02:07       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-29 09:02:07       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-29 09:02:07       18 阅读

热门阅读

  1. 【Spring】27 UrlResource:访问各种资源的通用工具

    2024-03-29 09:02:07       18 阅读
  2. 如何查看自己服务器的SSL证书?

    2024-03-29 09:02:07       18 阅读
  3. Vue.js:构建高效且灵活的Web应用的利器

    2024-03-29 09:02:07       18 阅读
  4. 显示器分辨率

    2024-03-29 09:02:07       19 阅读
  5. 「PHP系列」PHP echo/print语句、数据类型详解

    2024-03-29 09:02:07       22 阅读
  6. 配置SSH后 GitHub无法使用了

    2024-03-29 09:02:07       17 阅读