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

相关推荐
FirstFrost --sy32 分钟前
数据结构之二叉树
c语言·数据结构·c++·算法·链表·深度优先·广度优先
Chef_Chen36 分钟前
从0开始学习计算机视觉--Day08--卷积神经网络
学习·计算机视觉·cnn
weixin_4462608536 分钟前
Isaac Lab:让机器人学习更简单的开源框架
学习·机器人
Yingye Zhu(HPXXZYY)1 小时前
Codeforces 2021 C Those Who Are With Us
数据结构·c++·算法
liulilittle2 小时前
LinkedList 链表数据结构实现 (OPENPPP2)
开发语言·数据结构·c++·链表
我真不会起名字啊2 小时前
OpenSceneGraph(OSG)开发学习
学习
永日456702 小时前
学习日记-spring-day42-7.7
java·学习·spring
无聊的小坏坏2 小时前
三种方法详解最长回文子串问题
c++·算法·回文串
山河木马3 小时前
前端学习C++之:.h(.hpp)与.cpp文件
前端·javascript·c++
2401_891957313 小时前
list的一些特性(C++)
开发语言·c++