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、命名空间可以起别名

相关推荐
charade3121 小时前
【C语言】内存分配的理解
c语言·开发语言·c++
yuhouxiyang3 小时前
学习海康VisionMaster之路径提取
学习·计算机视觉
雾削木4 小时前
mAh 与 Wh:电量单位的深度解析
开发语言·c++·单片机·嵌入式硬件·算法·电脑
PLUS_WAVE5 小时前
CogCoM: A Visual Language Model with Chain-of-Manipulations Reasoning 学习笔记
学习·语言模型·大模型·cot·vlm·推理模型·reasoning
绵绵细雨中的乡音5 小时前
Linux进程学习【环境变量】&&进程优先级
linux·运维·学习
贺函不是涵5 小时前
【沉浸式求职学习day27】
学习
努力奋斗的小杨5 小时前
学习MySQL的第十二天
数据库·笔记·学习·mysql·navicat
枫叶20006 小时前
OceanBase数据库-学习笔记1-概论
数据库·笔记·学习·oceanbase
Ethon_王6 小时前
走进Qt--工程文件解析与构建系统
c++·qt
你可以叫我仔哥呀6 小时前
k8s学习记录(五):Pod亲和性详解
学习·容器·kubernetes