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

相关推荐
古月-一个C++方向的小白6 小时前
C++11之lambda表达式与包装器
开发语言·c++
tanyongxi668 小时前
C++ AVL树实现详解:平衡二叉搜索树的原理与代码实现
开发语言·c++
斯是 陋室10 小时前
在CentOS7.9服务器上安装.NET 8.0 SDK
运维·服务器·开发语言·c++·c#·云计算·.net
tju新生代魔迷11 小时前
C++:list
开发语言·c++
iFulling11 小时前
【计算机网络】第四章:网络层(上)
学习·计算机网络
HHRL-yx11 小时前
C++网络编程 5.TCP套接字(socket)通信进阶-基于多线程的TCP多客户端通信
网络·c++·tcp/ip
香蕉可乐荷包蛋11 小时前
AI算法之图像识别与分类
人工智能·学习·算法
tomato0911 小时前
河南萌新联赛2025第(一)场:河南工业大学(补题)
c++·算法
xiaoli232711 小时前
课题学习笔记1——文本问答与信息抽取关键技术研究论文阅读(用于无结构化文本问答的文本生成技术)
笔记·学习
人生游戏牛马NPC1号11 小时前
学习 Flutter (四):玩安卓项目实战 - 中
android·学习·flutter