• C++笔记


    1 C++概述
    1.1 C++两大编程思想
    1.1.1 面向对象
    1.1.2 泛型编程
    1.2 移植性和标准
    1.2.1 ANSI 在1998制定出C++第一套标准
    2 c++初识
    2.1 引入头文件 #include 标准输入输出流
    2.2 使用标准命名空间 using namespace std;
    2.3 标准输出流对象 cout << “…” << 1234 << 3.14 << endl;
    2.4 面向对象三大特性
    2.4.1 封装、继承、多态

    第一个c++程序:hello word

    #include 		//标准输入输出流  相当于stdio.h
    using namespace std; 	//使用标准命名空间
    
    //#include 
    //#include 
    
    //程序入口函数
    int main()
    {
    	//cout 标准输出流对象
    	//在c里是左移,在c++下有了新的寓意:用于cout后拼接输出的内容
    	//endl  ---end line  刷新缓冲区 并且换行
    	cout << "hello world!" << endl;
    
    	system("pause");	//阻塞
    	return EXIT_SUCCESS;	//返回正常退出
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    3 双冒号作用域运算符
    3.1 ::代表作用域 如果前面什么都不添加 代表全局作用域

    ::作用域

    #include 
    using namespace std;
    
    int atk = 1000;
    void test1 ()
    {	
    	int atk = 500;
    	cout << "局部 atk = " << atk << endl;
    	cout << "全局 atk = " << ::atk << endl;
    }
    
    int main()
    {
    	test1();
    	cout << "Hello World!" << endl;
    	
    	return EXIT_SUCCESS;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    4 namespace命名空间
    4.1 命名空间用途:解决名称冲突
    4.2 命名空间下可以存放 : 变量、函数、结构体、类…
    4.3 命名空间必须要声明在全局作用域
    4.4 命名空间可以嵌套命名空
    4.5 命名空间是开放的,可以随时将新成员添加到命名空间下
    4.6 命名空间可以匿名的
    4.7 命名空间可以起别名
    程序:
    game1.h

    #include 
    using namespace std;
    
    namespace KingGlory
    {
    	void goAtk();
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    game2.h

    #include 
    using namespace std;
    
    namespace LOL
    {
    	void goAtk();
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    game1.cpp

    #include "game1.h"
    
    void KingGlory::goAtk()
    {
    	cout << "王者荣耀攻击实现" << endl;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    game2.cpp

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

    namespace.cpp

    #include 
    using namespace std;
    #include "game1.h"
    #include "game2.h"
    
    //1、命名空间用途:解决名称冲突
    void test01()
    {
    	KingGlory::goAtk();
    	LOL::goAtk();
    }
    
    //2、命名空间下可以放 变量、函数、结构体、类...
    namespace A
    {
    	int m_A;
    	void func();
    	struct Person
    	{};
    	class Animal
    	{};
    }
    
    //3、命名空间必须要声明在全局作用域下
    void test2()
    {
    	//namespace B {}; 不可以命名到局部作用域
    }
    
    //4、命名空间可以嵌套命名空间
    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;
    }
    
    //5、命名空间是开放的,可以随时给命名空间添加成员
    namespace B
    {
    	int m_B = 20;
    }
    void test04()
    {
    	cout << "B空间下的m_A = " << B::m_A << endl;
    	cout << "B空间下的m_B = " << B::m_B << endl;
    }
    
    //6、命名空间可以是匿名的
    namespace
    {
    	int m_C = 100;
    	int m_D = 200;
    	//当写的命名空间是匿名的,相当于写了 static int m_C = 100; static int m_D = 200;
    }
    void test05()
    {
    	cout << "m_C = " << m_C << endl;
    	cout << "m_D = " << ::m_D << endl;
    }
    
    //7、命名空间可以起别名
    namespace veryLongName
    {
    	int m_E = 10000;
    }
    void test06()
    {
    	namespace veryShortName = veryLongName;
    	cout << "m_E = " << veryLongName::m_E << endl;
    	cout << "m_E = " << veryShortName::m_E << endl;
    }
    int main()
    {
    	//test01();
    	//test03();
    	//test04();
    	//test05();
    	test06();
    
    	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
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89

    5 using声明以及using编译指令
    5.1 using声明
    5.1.1 using KingGlory::sunwukongId
    5.1.2 当using声明与 就近原则同时出现,出错,尽量避免
    5.2 using编译指令
    5.2.1 using namespace KingGlory;
    5.2.2 当using编译指令 与 就近原则同时出现,优先使用就近
    5.2.3 当using编译指令有多个,需要加作用域 区分

    #include
    using namespace std;
    
    namespace KingGlory
    {
    	int sunwukongId = 1;
    }
    
    namespace LOL
    {
    	int sunwukongId = 3;
    }
    
    void test01()
    {
    	int sunwukongId = 2;
    
    	//1、using声明
    	//using KingGlory::sunwukongId ;  
    
    	//当using声明与 就近原则同时出现,出错,尽量避免
    	cout << sunwukongId << endl;
    
    }
    
    
    void test02()
    {
    	//int sunwukongId = 2;
    	//2、using编译指令
    	using namespace KingGlory;
    	using namespace LOL;
    	//当using编译指令  与  就近原则同时出现,优先使用就近
    	//当using编译指令有多个,需要加作用域 区分
    	cout << KingGlory::sunwukongId << endl;
    	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
    • 45

    6 C++对C语言增强以及扩展
    6.1 全局变量检测增强
    6.1.1 int a ;
    6.1.2 int a = 10; C下可以,C++重定义
    6.2 函数检测增强
    6.2.1 函数的返回值
    6.2.2 形参类型
    6.2.3 函数调用参数个数
    6.3 类型转换检测增强
    6.3.1 char * p = (char *)malloc(64) C++下必须等号左右一致类型
    6.4 struct 增强
    6.4.1 C++可以在结构体中放函数
    6.4.2 创建结构体变量 可以简化关键字struct
    6.5 bool数据类型扩展
    6.5.1 C++才有bool类型
    6.5.2 代表真 — 1 true 假 ---- 0 false
    6.5.3 sizeof = 1
    6.6 三目运算符增强
    6.6.1 C语言下返回的是值
    6.6.2 C++语言下返回的是变量
    6.7 const增强
    6.7.1 C语言下
    6.7.1.1 全局const 直接修改 失败 间接修改 语法通过,运行失败
    6.7.1.2 局部 const 直接修改 失败 间接修改 成功
    6.7.2 C++语言下
    6.7.2.1 全局 const 和C结论一样
    6.7.2.2 局部 const 直接修改失败 间接修改 失败
    6.7.2.3 C++const可以称为常量

    C++对C语言增强以及扩展.c

    #define _CRT_SECURE_NO_WARNINGS
    #include
    #include
    #include
    
    //1、全局变量检测增强
    int a;
    int a = 10;
    
    //2、函数检测增强  返回值没有检测  形参类型没有检测   函数调用参数个数没有检测
    getRectS( w  , h)
    {
    
    	return w *h;
    }
    void test01()
    {
    	printf("%d\n", getRectS(10, 10, 10));
    }
    
    
    //3、类型转换检测增强
    void test02()
    {
    	char * p = malloc(64);
    }
    
    
    //4、struct增强
    struct Person
    {
    	int age;
    	//void func(); C语言下 结构体不可以有函数
    };
    
    void test03()
    {
    	struct Person p; //创建结构体变量时候,必须加关键字struct
    	p.age = 100;
    }
    
    
    //5、bool类型扩展  C语言下 没有这个类型
    //bool a;
    
    
    //6、三目运算符增强
    void test04()
    {
    	//?:
    	int a = 10;
    	int b = 20;
    
    	printf("ret = %d\n", a > b ? a : b);
    
    	*(a > b ? &a : &b) = 100;  //C语言下 返回的是值  20 = 100
    
    	printf("a = %d\n", a);
    	printf("b = %d\n", b);
    
    }
    
    
    //7、const增强
    //全局const
    const int m_A = 100; // 受到常量区保护,运行修改失败
    
    void test05()
    {
    	//m_A = 200;
    	//int * p = &m_A;
    	//*p = 200;
    
    	//局部const
    	const int m_B = 100; //分配到栈上
    	//m_B = 200;
    	int * p = &m_B;
    	*p = 200;
    
    	printf("%d\n", m_B);
    
    	//int arr[m_B]; 在C语言下 const是伪常量,不可以初始化数组
    
    }
    
    
    int main(){
    	
    	//test01();
    	//test04();
    	test05();
    
    
    	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
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96

    C++对C语言增强以及扩展.cpp

    #define _CRT_SECURE_NO_WARNINGS
    #include
    using namespace std;
    
    //1、全局变量检测增强  C++检测出重定义
    int a;
    //int a = 10;
    
    //2、函数检测增强 返回值检测、形参类型检测、函数调用参数个数
    int getRectS(int w,int h)
    {
    
    	return w *h;
    }
    void test01()
    {
    	printf("%d\n", getRectS(10, 10));
    }
    
    
    //3、类型转换检测增强
    void test02()
    {
    	char * p = (char *)malloc(64);
    }
    
    //4、struct增强  C++可以放函数,创建结构体变量,可以简化关键字 struct
    struct Person
    {
    	int age;
    	void func()
    	{
    		age++;
    	}
    };
    
    void test03()
    {
    	Person p;
    	p.age = 17;
    	p.func();
    	cout << "p的age = " << p.age << endl;
    }
    
    //5、bool类型扩展  C语言下 没有这个类型  C++有bool类型
    bool flag = true; // bool类型 代表  真和假   true  ---- 真(1)    false  ---- 假(0)
    
    void test04()
    {
    	cout << sizeof(bool) << endl; //结果是1个字节
    	//flag = false;
    	//flag = 100; //将非0的数都转为1
    	cout << flag << endl;
    }
    
    //6、三目运算符增强
    void test05()
    {
    	//?:
    	int a = 10;
    	int b = 20;
    
    	printf("ret = %d\n", a > b ? a : b);
    
    	(a < b ? a : b )= 100; // C++下返回的是变量  b = 100
    
    	printf("a = %d\n", a);
    	printf("b = %d\n", b);
    }
    
    
    //7、const增强
    //全局const   和C语言结论一致
    const int m_A = 100;
    void test06()
    {
    	//m_A = 200;
    	//int * p = (int *)&m_A;
    
    	//*p = 200;
    
    
    	//局部const
    	const int m_B = 100;
    	//m_B = 200;
    	int * p = (int *)&m_B;
    	*p = 200;
    	cout << "m_B = " << m_B << endl;
    
    	int arr[m_B]; //C++下const修饰的变量 称为常量 ,可以初始化数组
    
    }
    
    
    int main(){
    	//test01();
    	//test03();
    	//test04();
    	//test05();
    	test06();
    
    
    	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
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105

    7 const 链接属性
    7.1 C语言下const修饰的全局变量默认是外部链接属性

    test.c

    const int g_a = 1000;
    
    • 1

    C语言下const修饰的全局变量默认是外部链接属性.c

    #define _CRT_SECURE_NO_WARNINGS
    #include
    #include
    #include
    
    int main(){
    
    	extern const int g_a; 
    
    	printf("g_a = %d\n", g_a);
    
    	system("pause");
    	return EXIT_SUCCESS;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    7.2 C++下const修饰的全局变量默认是内部链接属性,可以加extern 提高作用域

    test.cpp

    extern const int g_b = 1000;//默认是内部链接属性 可以加关键字 extern 提高作用域
    
    • 1

    C++下const修饰的全局变量默认是内部链接属性.cpp

    #define _CRT_SECURE_NO_WARNINGS
    #include
    using namespace std;
    
    int main(){
    
    	extern const int g_b;
    
    	cout << "g_b = " << g_b << endl;;
    
    	system("pause");
    	return EXIT_SUCCESS;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    8 const分配内存情况
    8.1 对const变量 取地址 ,会分配临时内存
    8.2 使用普通变量 初始化 const变量
    8.3 对于自定义数据类型

    const分配内存情况.cpp

    #define _CRT_SECURE_NO_WARNINGS
    #include
    using namespace std;
    #include 
    
    //1、对const变量 取地址 ,会分配临时内存  
    void test01()
    {
    	const int a = 10;
    	int * p = (int *)&a;
    }
    
    //2、使用普通变量  初始化 const变量
    void test02()
    {
    	int a = 10;
    	const int b = a;
    
    	int *p = (int *)&b;
    	*p = 1000;
    
    	cout << "b = " << b << endl;
    
    }
    
    //3、对于自定义数据类型 
    struct Person
    {
    	string m_Name;
    	int m_Age;
    };
    void test03()
    {
    	const Person p = {};
    	//p.m_Age = 10;
    
    	Person * pp = (Person *)&p;
    	(*pp).m_Name = "Tom";
    	pp->m_Age = 10;
    
    	cout << "姓名: " << p.m_Name << " 年龄: " << p.m_Age << endl;
    }
    
    
    int main(){
    
    	//test02();
    	test03();
    	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

    9 尽量用const代替define
    9.1 define出的宏常量,没有数据类型、不重视作用域
    10 引用
    10.1 目的:起别名
    10.2 语法: 类型(与原名类型必须一致) &别名 = 原名
    10.3 引用必须要初始化
    10.4 引用一旦初始化后,就不可以引向其他变量
    10.5 建立对数组引用
    10.5.1 直接建立引用
    10.5.2 int arr[10];
    10.5.3 int(&pArr)[10] = arr;
    10.6 先定义出数组类型,再通过类型 定义引用
    10.6.1 typedef int(ARRAY_TYPE)[10];
    10.6.2 ARRAY_TYPE & pArr2 = arr;

    引用.cpp

    #define _CRT_SECURE_NO_WARNINGS
    #include
    using namespace std;
    
    //引用基本语法:  类型  &别名 = 原名
    void test01()
    {
    	int a = 10;
    	int &b = a;
    
    	b = 100;
    	cout << "a = " << a << endl;
    	cout << "b = " << b << endl;
    }
    
    
    void test02()
    {
    	int a = 10;
    	//int &b; //引用必须要初始化
    
    	int &b = a;
    
    	//引用一旦初始化后,就不可以引向其他变量
    
    	int c = 100;
    
    	b = c; // 赋值
    
    	cout << "a = " << a << endl;
    	cout << "b = " << b << endl;
    	cout << "c = " << c << endl;
    }
    
    //对数组建立引用
    void test03()
    {
    	//1、直接建立引用
    	int arr[10];
    	int(&pArr)[10] = arr;
    
    	for (int i = 0; i < 10; i++)
    	{
    		arr[i] = 100 + i;
    	}
    	for (int i = 0; i < 10; i++)
    	{
    		cout << pArr[i] << endl;
    	}
    
    
    	//2、先定义出数组类型,再通过类型 定义引用
    	typedef int(ARRAY_TYPE)[10];
    	//类型  &别名 = 原名
    	ARRAY_TYPE & pArr2 = arr;
    
    	for (int i = 0; i < 10; i++)
    	{
    		cout << pArr2[i] << endl;
    	}
    
    }
    
    int main(){
    
    	//test01();
    	//test02();
    	test03();
    
    	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

    11 参数的传递方式
    11.1 值传递
    11.2 地址传递
    11.3 引用传递
    12 注意事项
    12.1 引用必须引一块合法内存空间
    12.2 不要返回局部变量的引用
    12.3 当函数返回值是引用时候,那么函数的调用可以作为左值进行运算

    参数的传递方式.cpp

    #define _CRT_SECURE_NO_WARNINGS
    #include
    using namespace std;
    
    //1、值传递
    void mySwap01(int a  , int b)
    {
    	int temp = a;
    	a = b;
    	b = temp;
    
    	/*cout << ":::a = " << a << endl;
    	cout << ":::b = " << b << endl;*/
    }
    
    //2、地址传递
    void mySwap02(int *a, int *b)
    {
    	int temp = *a;
    	*a = *b;
    	*b = temp;
    }
    
    //3、引用传递
    void mySwap03(int &a , int &b) // int &a = a; int &b = b;
    {
    	int temp = a;
    	a = b;
    	b = temp;
    }
    
    void test01()
    {
    	int a = 10;
    	int b = 20;
    	//mySwap01(a, b);
    	//mySwap02(&a, &b);
    
    	mySwap03(a, b);
    
    	cout << "a = " << a << endl;
    	cout << "b = " << b << endl;
    }
    
    int& func()
    {
    	int a = 10;
    	return a;
    }
    
    //引用注意事项
    void test02()
    {
    	//1、引用必须引一块合法内存空间
    	//int &a = 10;
    
    	//2、不要返回局部变量的引用
    	int &ref = func();
    	cout << "ref = " << ref << endl;
    	cout << "ref = " << ref << endl;
    }
    
    int& func2()
    {
    	static int a = 10;
    	return a;
    }
    
    void test03()
    {
    	int &ref = func2();
    	cout << "ref = " << ref << endl;
    	cout << "ref = " << ref << endl;
    	cout << "ref = " << ref << endl;
    	cout << "ref = " << ref << endl;
    	//当函数返回值是引用,那么函数的调用可以作为左值
    	func2() = 1000;
    
    	cout << "ref = " << ref << endl;
    
    
    }
    
    
    int main(){
    	//test01();
    	//test02();
    	test03();
    
    
    
    	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
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94

    13 指针的引用
    13.1 利用引用可以简化指针
    13.2 可以直接用同级指针的 引用 给同级指针分配空间

    #define _CRT_SECURE_NO_WARNINGS
    #include
    using namespace std;
    
    struct Person
    {
    	int age;
    };
    
    void allocateSpace(Person ** p)
    {
    	//p指向指针的指针    *p  指针 指向的是person 本体   **p  person本体
    	*p = (Person *)malloc(sizeof(Person));
    	(*p)->age = 10;
    
    }
    
    void test01()
    {
    	Person * p = NULL;
    	allocateSpace(&p);
    
    	cout << "p.age = " << p->age <<  endl;
    }
    
    
    
    void allocateSpace2(Person* &pp) // Person * &pp = p;
    {
    	pp = (Person *)malloc(sizeof(Person));
    	pp->age = 20;
    }
    
    void test02()
    {
    	Person *p = NULL;
    	allocateSpace2(p);
    	cout << "p.age = " << p->age << 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
    • 45
    • 46
    • 47

    14 常量的引用
    14.1 const int &ref = 10;
    14.2 // 加了const之后, 相当于写成 int temp = 10; const int &ref = temp;
    14.3 常量引用的使用场景 修饰函数中的形参,防止误操作

    #include 
    using namespace std;
    
    void test01()
    {
    	const int& ref = 10;	//加了const,相当于写成 int temp = 10;    const int &ref = temp;
    	
    	int *p = (int *) & ref;
    	*p = 10000;
    
    	cout << "ref = " << ref << endl;
    }
    
    void showValue(const int &a)
    {
    	//a = 100000;
    	cout << "a = " << a << endl;
    
    }
    
    //常量引用的使用场景,修饰函数中的形参,防止误操作
    void test02()
    {
    	int a = 100;
    	showValue(a);
    	cout << "::a = " << a << endl;
    }
    
    int main()
    {
    	test01();
    	test02();
    
    	return EXIT_FAILURE;
    }
    
    • 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
  • 相关阅读:
    Textbooks Are All You Need II: phi-1.5 technical report
    二十四节气—立秋,文案、海报分享。
    福建福州大型钢结构件3D扫描全尺寸三维测量平面度平行度检测-CASAIM中科广电
    【Java面试】面试自閟了!工作5年的小伙伴今天面试被吊打问我,并行和并发有什么区别?
    Java面试挂在线程创建后续,不要再被八股文误导了!创建线程的方式只有1种
    设计模式之代理模式
    CMake常用命令(一) cmake_minimum_required
    《算法通关村第二关——终于学会链表反转了》
    高校社团管理系统的设计与实现
    Scrapy框架介绍
  • 原文地址:https://blog.csdn.net/m0_61511416/article/details/128125317