C++学习Day01之namespace命名空间

目录

  • 一、程序及输出
    • [1.1 命名空间用途: 解决名称冲突](#1.1 命名空间用途: 解决名称冲突)
    • [1.2 命名空间内容](#1.2 命名空间内容)
    • [1.3 命名空间必须要声明在全局作用域下](#1.3 命名空间必须要声明在全局作用域下)
    • [1.4 命名空间可以嵌套命名空间](#1.4 命名空间可以嵌套命名空间)
    • [1.5 命名空间开放,可以随时给命名空间添加新的成员](#1.5 命名空间开放,可以随时给命名空间添加新的成员)
    • [1.6 命名空间可以是匿名的](#1.6 命名空间可以是匿名的)
    • [1.7 命名空间可以起别名](#1.7 命名空间可以起别名)
  • 二、分析与总结

一、程序及输出

1.1 命名空间用途: 解决名称冲突

game1.h

c 复制代码
#include <iostream>
using namespace std;


namespace KingGlory
{
	void goAtk();
}

game1.cpp

c 复制代码
#include "game1.h"

void KingGlory::goAtk()
{
	cout << "王者荣耀攻击实现" << endl;
}

game2.h

c 复制代码
#include <iostream>
using namespace std;


namespace LOL
{
	void goAtk();
}

game2.cpp

c 复制代码
#include "game2.h"

void  LOL::goAtk()
{
	cout << "LOL攻击实现" << endl;

}

main.cpp

c 复制代码
#include<iostream>
using namespace std;
#include "game1.h"
#include "game2.h"

//1、命名空间用途: 解决名称冲突
void test01()
{
	KingGlory::goAtk();

	LOL::goAtk();
}
int main(){
	test01();
	system("pause");
	return EXIT_SUCCESS;
}

输出:

1.2 命名空间内容

c 复制代码
namespace A
{
	int m_A;
	void func();
	struct Person
	{};
	class Animal
	{};
}

编译器没有报错提示。

1.3 命名空间必须要声明在全局作用域下

1.4 命名空间可以嵌套命名空间

c 复制代码
#include<iostream>
using namespace std;
namespace B
{
	int m_A = 10;
	namespace C
	{
		int m_A = 20;
	}
}
void test03()
{
	cout << "B空间下的m_A = " << B::m_A << endl;
	cout << "C空间下的m_A = " << B::C::m_A << endl;
}
int main(){
	test03();
	system("pause");
	return EXIT_SUCCESS;
}

输出:

1.5 命名空间开放,可以随时给命名空间添加新的成员

c 复制代码
#include<iostream>
using namespace std;
namespace B
{
	int m_A = 10;
	namespace C
	{
		int m_A = 20;
	}
}
namespace B
{
	int m_B = 100;
}
void test04()
{
	cout << "B空间下的m_A = " << B::m_A << endl;
	cout << "B空间下的m_B = " << B::m_B << endl;
}
int main(){
	test04();
	system("pause");
	return EXIT_SUCCESS;
}

输出:

1.6 命名空间可以是匿名的

c 复制代码
#include<iostream>
using namespace std;
namespace
{
	int m_C = 1000;
	int m_D = 2000; 
	//当写的命名空间的匿名的,相当于写了  static int m_C = 1000; static int m_D = 2000;
}

void test05()
{
	cout << "m_C = " << m_C   << endl;
	cout << "m_D = " << ::m_D << endl;
}
int main(){
	test05();
	system("pause");
	return EXIT_SUCCESS;
}

输出:

1.7 命名空间可以起别名

c 复制代码
#include<iostream>
using namespace std;
namespace veryLongName
{
	int m_E = 10000;
	void func()
	{
		cout << "aaa" << endl;
	}
}

void test06()
{
	namespace veryShortName = veryLongName;
	cout << veryShortName::m_E << endl;
	cout << veryLongName::m_E << endl;
	veryLongName::func();
}
int main(){
	test06();
	system("pause");
	return EXIT_SUCCESS;
}

输出:


二、分析与总结

1、命名空间用途: 解决名称冲突

2、命名空间内容:可以放 变量、函数、结构体、类...

3、命名空间 必须要声明在全局作用域下

4、命名空间可以嵌套命名空间

5、命名空间是开放的,可以随时给命名空间添加新的成员

6、命名空间可以是匿名的

7、命名空间可以起别名

相关推荐
奔波霸的伶俐虫1 天前
windows docker desktop 安装修改镜像学习
学习·docker·容器
散峰而望1 天前
【算法竞赛】顺序表和vector
c语言·开发语言·数据结构·c++·人工智能·算法·github
cpp_25011 天前
B3927 [GESP202312 四级] 小杨的字典
数据结构·c++·算法·题解·洛谷
Cx330❀1 天前
《C++ 递归、搜索与回溯》第2-3题:合并两个有序链表,反转链表
开发语言·数据结构·c++·算法·链表·面试
BullSmall1 天前
《逍遥游》
学习
小六子成长记1 天前
【C++】:多态的实现
开发语言·c++
奔波霸的伶俐虫1 天前
spring boot集成kafka学习
spring boot·学习·kafka
CCPC不拿奖不改名1 天前
面向对象编程:继承与多态+面试习题
开发语言·数据结构·python·学习·面试·职场和发展
chen_2271 天前
动态桌面方案
c++·qt·ffmpeg·kanzi
GHL2842710901 天前
通义千问的 Function Call - demo学习
学习·ai·ai编程