C++基础学习DAY1-03 namespace的使用

/*game1.h*/
#pragma once
#include <iostream>
using namespace std;
namespace LOL
{
   void goAtk();
}
/*game1.cpp*/
#include "game1.h"
void LOL::goAtk()
{
     cout << "LOL的攻击实现" << endl;
}
/*game2.h*/
#pragma once
#include <iostream>
using namespace std;
namespace KingGlory
{
    void goAtk();
}
/*game2.cpp*/
#include "game2.h"
void KingGlory::goAtk()
{
     cout << "王者农药的攻击实现" << endl;
}
/*namespace的使用。cpp*/
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include "game1.h"
#include "game2.h"
using namespace std;
//namespace命名空间 主要解决命名冲突的问题
//1、命名空间下 可以放函数、变量、结构体、类
namespace A
{
	void func() {};
 	int m_A;
 struct Person
 {
 };
 class Animal {};
 namespace B
 {
  	int m_A=100;
 }
}
//2、命名空间必须定义在全局的作用域下
//3、命名空间可以嵌套命名空间
void test02()
{
 	cout << "作用域B下的m_A为" << A::B::m_A <<  endl;
}
//4、命名空间是开放的 可以随时往原来的命名空间添加内容
namespace A  //此A命名空间会与上边的A空间合并
{
	 int m_B = 200;
}
void test03()
{
	 cout << "命名空间A下的m_A为" << A::m_A << "  m_B为" << A::m_B << endl;
}
//5、无名 匿名命名空间 
namespace
{//相当于static int m_C;static int m_D;
 	int m_C;
	int m_D;
}
//6、 命名空间起别名
namespace veryLongName
{
 	int m_A;
}
void test04()
{
 	namespace veryShortName = veryLongName;
 	cout << veryLongName::m_A << endl;
 	cout << veryShortName::m_A << endl;
}
int main()
{
 	 //LOL::goAtk();
	 //KingGlory::goAtk();
 	 //test02();
	 //test03();
	 test04();
	 system("pause");
 	return EXIT_SUCCESS;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值