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

相关推荐
wxin_VXbishe9 分钟前
springboot居家养老管理系统-计算机毕业设计源码55953
java·c++·spring boot·python·spring·django·php
ULTRA??19 分钟前
归并排序算法实现,kotlin,c++,python
c++·python·kotlin
deng-c-f24 分钟前
C/C++内置库函数(5):值/引用传递、移动构造、以及常用的构造技巧
开发语言·c++
qq_3106585124 分钟前
mediasoup源码走读(十)——producer
服务器·c++·音视频
Tipriest_31 分钟前
C++ Python使用常用库时如何做欧拉角 ⇄ 四元数转换
c++·python·四元数·欧拉角
小尧嵌入式34 分钟前
C语言中的面向对象思想
c语言·开发语言·数据结构·c++·单片机·qt
fpcc41 分钟前
跟我学C++中级篇——循环展开的分析
c++·优化
埃伊蟹黄面1 小时前
算法 --- hash
数据结构·c++·算法·leetcode
白帽子凯哥哥2 小时前
转行网络安全学习计划与报班建议
学习·安全·web安全·网络安全·渗透测试·漏洞挖掘·网安培训
ULTRA??2 小时前
Informed RRT*实现椭圆启发式采样
c++·算法