当前位置:   article > 正文

C++学习笔记_c++ 命名空间 全局变量

c++ 命名空间 全局变量

文章目录

1、#include< iostream >

预编译指令,引入头文件iostream

1.1 using namespace std;

使用标准命名空间
namespace命名空间关键字。
命名空间是C++的一种机制,用来把单个标识符下的大量有逻辑联系的程序实体组合到一起。此标识符作为组群的名字。
std打开一个房间,使得输入和输出流等关键字都是从这个房间出去的。

1.2 cout << “hello …” << endl;

printf功能,输出字符串”hello wrold”。
cout是c++中的标准输出流,endl是输出换行并刷新缓冲区。

1.3 system(“pause”)

起到阻塞功能,这样程序运行完不会转跳,防止屏幕一闪而过

1.4 retrun 0

正常结束返回

#define _CRT_SECURE_NO_WARNINGS
#include <iostream> //标准输入输出流  in输入  out 输出
//using namespace std;//使用命名空间 std 打开一个叫std房间

//函数入口地址
int main()
{
	// cout 标准的输出
	// <<  左移运算符
	// endl 结束换行
	std::cout << "hello world" << 123456 << std::endl;

	system("pause"); //阻塞功能
	return EXIT_SUCCESS; //返回正常退出
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

2 ::双冒号作用域运算符

2.1 全局作用域 直接加::(识别全局变量)

如果有两个同名变量,一个是全局变量,另一个是局部变量,那么局部变量在其作用域内具有较高的优先权,它将屏蔽全局变量。

//全局变量
int a = 10;
void test(){
	//局部变量
	int a = 20;
	//全局a被隐藏
	cout << "a:" << a << endl;
}
//输出输出结果是a:20,在test语句中输出内部定义的局部变量
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

作用域运算符可以用来解决局部变量与全局变量的重名问题,即在局部变量的作用域内,通过**::访问被屏蔽同名的全局变量**

//全局变量
int a = 10;
//1. 局部变量和全局变量同名
void test(){
	int a = 20;
	//打印局部变量a
	cout << "局部变量a:" << a << endl;
	//打印全局变量a
	cout << "全局变量a:" << ::a << endl;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;

int atk = 200;
void test01()
{
	int atk = 100;
	cout << "攻击力为 : " << atk << endl;
	//双冒号 作用域运算符  ::全局作用域
	cout << "全局攻击力为 : " << ::atk << endl;
}

int main()
{
	test01();
	system("pause");
	return EXIT_SUCCESS;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

3 namespace 命名空间

3.1 用途 解决名称冲突问题

为避免在大规模程序的设计中,以及在程序员使用各种各样的C++库时,标识符的命名发生冲突,标准C++引入关键字namespace(命名空间/名字空间/名称空间),更好地控制标识符的作用域
主要功能:解决命名冲突的问题
game1.h

#include <iostream>
using namespace std;

namespace LOL
{
	void goAtk();
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

game1.cpp

#include "game1.h"
void LOL::goAtk()
{
	cout << "LOL攻击实现" << endl;
}
  • 1
  • 2
  • 3
  • 4
  • 5

game2.h

#include <iostream>
using namespace std;

namespace KingGlory
{
	void goAtk();
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

game2.cpp

#include "game2.h"
void KingGlory::goAtk()
{
	cout << "王者农药攻击实现" << endl;
}
  • 1
  • 2
  • 3
  • 4
  • 5
3.2 必须在全局作用域下声明

错误写法!!!

void test(){
	namespace A{
		int a = 10;
	}
	namespace B{
		int a = 20;
	}
	cout << "A::a : " << A::a << endl;
	cout << "B::a : " << B::a << endl;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
3.3 命名空间下可以放入 函数、变量、结构体、类
namespace A
{
	void func();
	int m_A = 20;
	struct Person
	{
	};
	class Animal{};
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
3.4 命名空间可以嵌套命名空间
namespace A
{
	namespace B
	{
		int m_A = 10;
	}
}
void test()
{
	cout << "作用域B下的m_A为: " << A::B::m_A << endl;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
3.5 命名空间是开放的,可以随时加入新的成员
namespace A
{
	int a = 10;
}
namespace A
{
	void func()
	{
		cout << "hello namespace!" << endl;
	}
}
void test()
{
	cout << "A::a : " << A::a << endl;
	A::func();
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
3.6 匿名命名空间 static(很少使用)
namespace
{
	int m_C = 0;
	int m_D = 0;
}
//当写了无名命名空间,相当于写了 static int m_C ; static int m_D;
//只能在当前文件内使用
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

无名命名空间,意味着命名空间中的标识符只能在本文件内访问,相当于给这个标识符加上了static,使得其可以作为内部连接

3.7 可以起别名
namespace veryLongName
{
	int m_A = 0;
}
void test()
{
	//起别名
	namespace veryShortName = veryLongName;
	cout << veryLongName::m_A << endl;
	cout << veryShortName::m_A << endl;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
#include "game1.h"
#include "game2.h"

//namespace命名空间主要用途 用来解决命名冲突的问题
void test01()
{
	LOL::goAtk();
	KingGlory::goAtk();
}

//1、命名空间下 可以放函数、变量、结构体、类
namespace A
{
	void func();
	int m_A = 20;
	struct Person
	{
	};
	class Animal{};
	namespace B
	{
		int m_A = 10;
	}
}
//2、命名空间必须定义在全局作用域下

//3、命名空间可以嵌套命名空间

void test02()
{
	cout << "作用域B下的m_A为: " << A::B::m_A << endl;
}

//4、命名空间是开放的,可以随时往原先的命名空间添加内容
namespace A  //此A命名空间会和上面的命名空间A进行合并
{
	int m_B = 1000;
}

void test03()
{
	cout << "A::下的m_A为" << A::m_A << " m_B为: " << A::m_B << endl;
}

//5、无名、匿名命名空间
namespace
{
	int m_C = 0;
	int m_D = 0;
}
//当写了无名命名空间,相当于写了 static int m_C ; static int m_D;
//只能在当前文件内使用

//6、命名空间可以起别名
namespace veryLongName
{
	int m_A = 0;
}
void test04()
{
	//起别名
	namespace veryShortName = veryLongName;
	cout << veryLongName::m_A << endl;
	cout << veryShortName::m_A << endl;
}

int main(){
	//test01();

	//test02();

	//test03();

	test04();

	system("pause");
	return EXIT_SUCCESS;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81

4 using声明和using编译指令

4.1 using LOL:: sunwukongID;
4.2 如果局部范围内还有 sunwukongID,会出现二义性问题,要注意避免
4.3 编译指令
4.4 using namespace LOL
4.5 如果局部范围内还有 sunwukongID ,使用局部的ID
4.6 如果打开多个房间,那么也要注意二义性问题
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;

namespace KingGlory
{
	int sunwukongId = 10;
}

void test01()
{
	int sunwukongId = 20;//局部变量 

	//using 声明  注意避免二义性问题
	//写了using声明后  下面这行代码说明以后看到的sunwukongId 是用KingGlory下的
	//但是  编译器又有就近原则
	//二义性,没法区分sunwukongID使用的是那个 
	//using KingGlory::sunwukongId;

	cout << sunwukongId << endl;
}

//using编译指令
namespace LOL
{
	int sunwukongId = 30;
}
void test02()
{
	//int sunwukongId = 20;
	//using编译指令
	using namespace KingGlory; //打开王者荣耀房间
	using namespace LOL;//打开LOL房间
	//如果打开多个房间,也要避免二义性问题
	cout << LOL::sunwukongId << endl;
}

int main(){

	//test01();
	test02();
	system("pause");
	return EXIT_SUCCESS;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44

5 C++对C语言增强

5.1 全局变量检测增强
//C
int a;//没有赋值,当做声明
int a = 10;//赋值,当做定义

//C++
//int a;
int a = 10;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
5.2 函数检测增强(C++中所有的变量和函数都必须有类型)
5.2.1 参数类型检测
5.2.2 返回值检测
5.2.3 传参个数检测
//C: 函数检测增强 
int getRectS(w, h)
{
}
void test02()
{
	getRectS(10, 10, 10);
}

//C++: 函数检测增强 ,参数类型增强,返回值检测增强,函数调用参数检测增强
int getRectS(int w, int h)
{
	return w*h;
}
void test02()
{
	getRectS(10, 10);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
5.3 类型转换检测增强
5.3.1 malloc返回void* ,C中可以不用强转,C++必须强转
//C: 类型转换检测增强
void test()
{
	char * p = malloc(sizeof(64)); //malloc返回值是void*
}

//C++: 类型转换检测增强
void test()
{
	char * p = (char*)malloc(sizeof(64)); //malloc返回值是void*
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

malloc函数详细解释

5.4 struct增强
5.4.1 C中不许有函数 C++可以
5.4.2 使用C必须加关键字 struct ,C++可以不加
//C: struct 增强
struct Person
{
	int m_Age;
	//void plusAge(); //c语言中struct不可以加函数
};
void test()
{
	struct Person p1; //使用时候必须加入struct关键字
}

//C++: struct 增强
struct Person
{
	int m_Age;
	void plusAge(){ m_Age++; }; //c++中struct可以加函数
};
void test()
{
	Person p1; //使用时候可以不加struct关键字
	p1.m_Age = 10;
	p1.plusAge();
	cout << p1.m_Age << endl;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
5.5 bool数据类型增强
5.5.1 C没有 C++有
5.5.2 true 真 false假
5.5.3 sizeof 1
//C: bool类型增强 C语言中没有bool类型
//bool flag;

//C++: bool类型增强 C语言中没有bool类型
bool flag = true; //只有真或假 true代表 真(非0)  false 代表假(0)
void test()
{
	cout << sizeof(bool) << endl;//值是1

	flag = 100;
	//bool类型 非0的值 转为 1 ,0就转为0
	cout << flag << endl;//值是1
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
5.6 三目运算符增强
5.6.1 C中返回的是值
5.6.2 C++中返回的是变量
//C: 三目运算符增强
void test()
{
	int a = 10;
	int b = 20;

	printf("ret = %d \n", a > b ? a : b);

	//a > b ? a : b = 100; // 20 = 100 C语言返回的是值

	//C语言中想模仿C++写
	*(a > b ? &a : &b) = 100;
	printf("a = %d ,b = %d \n", a, b);
}

//C++: 三目运算符增强
void test()
{
	int a = 10;
	int b = 20;

	cout << "ret = " << (a > b ? a : b) << endl;//返回20
	
	(a > b ? a : b) = 100; //b = 100 C++中返回的是变量,加小括号
	 
	cout << "a = " << a << endl;//10
	cout << "b = " << b << endl;//100
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
5.7 const增强
5.7.1 C中const是伪常量,可以通过指针修改
5.7.2 C++中const会放入到符号表中
5.7.3 C中const默认是外部链接,C++中const默认是内部链接
5.7.4 const分配内存情况
5.7.4.1 对变量取地址,会分配临时内存
5.7.4.2 extern关键字下的const会分配内存
5.7.4.3 用普通变量初始化const变量
5.7.4.4 自定义数据类型会分配内存
5.7.5 尽量用const代替define
5.7.5.1 define宏没有作用域概念
5.7.5.2 define宏常量没有类型

6 引用基本语法
6.1.1 用途起别名
6.1.2 Type &别名 = 原名
6.1.3 引用必须初始化
6.1.4 一旦初始化后 不能修改
6.1.5 对数组建立引用
6.2 参数3种传递方式
6.2.1 值传递
6.2.2 地址传递
6.2.3 引用传递
6.3 注意事项,不要返回局部变量的引用
6.4 如果函数返回值是引用,那么函数的调用可以作为左值
6.5 引用的本质 就是一个指针常量
7 指针的引用
7.1 用一级指针引用 可以代替二级指针
8 常量引用
8.1 使用场景 修饰形参为只读
8.2 const int &a = 10;会分配内存

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号