c++基础-const和引用(Type &别名 = 原名)

01 Hello World

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

//函数入口地址
int main()
{
	//cout b标准的输出
	// << 左移运算符 ,链接字符串
	// endl 结束换行
	cout << "hello world" << 123456 << endl;
	system("pause"); //阻塞功能
	return EXIT_FAILURE;
}

在这里插入图片描述

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

#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_FAILURE;
}

在这里插入图片描述

03 namespace的使用

game1.h

#include<iostream>
using namespace std;

namespace LOL
{
	void goAtk();
}

game2.h

#include<iostream>
using namespace std;

namespace KingGlory
{
	void goAtk();
}

game1.cpp

#include "game1.h"

void LOL::goAtk()
{
	cout << "LOL攻击实现" << endl;
}

game2.cpp

#include "game2.h"

void KingGlory::goAtk()
{
	cout << "王者农药攻击实现" << endl;
}

namespace的使用.cpp

#include <iostream> 
using namespace std;
#include "game1.h"
#include "game2.h"

//namespace 命名空间主要用途:用来解决命名冲突的问题
void tset01()
{
	LOL::goAtk();
	KingGlory::goAtk();
}
//1.命名空间下  可以放 函数,变量,结构体,类
namespace A
{
	void func();
	int m_A;
	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进行合并
{
	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_FAILURE;
}

namespace 命名空间主要用途:用来解决命名冲突的问题
test01():

void test01()
{
	LOL::goAtk();
	KingGlory::goAtk();
}

在这里插入图片描述
1.命名空间下 可以放 函数,变量,结构体,类

namespace A
{
	void func();
	int m_A;
	struct  Person
	{

	};
	class Animal{};
	namespace B
	{
		int m_A = 10;
	}
}

2.命名空间必须定义在全局作用域下
3.命名空间可以嵌套命名空间
test02():

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

在这里插入图片描述

4.命名空间是开放的,可以随时往原先的命名空间添加内容

namespace 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.命名空间可以起别名
test04():

namespace veryLongName
{
	int m_A = 0;
}
void test04()
{
	//起别名
	namespace veryShortName = veryLongName;
	cout << veryLongName::m_A << endl;
	cout << veryShortName::m_A << endl;
}

在这里插入图片描述

04 using 声明和using编译指令

using声明,存在二义性问题

#include <iostream> 
using namespace std;

namespace KingGlory
{
	int sunwukongId = 10;
}

void test01()
{
	int sunwukongId = 20;
	//using 声明
	//写了using声明后,下面这段diam说明以后看到的sunwukongId是用KingGlory下的
	//但是 编译器又有就近原则
	//二义性
	using KingGlory::sunwukongId;
	cout << sunwukongId << endl;
}

int main()
{
	test01();
	system("pause");
	return EXIT_FAILURE;
}

在这里插入图片描述
using编译指令

#include <iostream> 
using namespace std;

namespace KingGlory
{
	int sunwukongId = 10;
}

//void test01()
//{
//	int sunwukongId = 20;
//	//using 声明
//	//写了using声明后,下面这段diam说明以后看到的sunwukongId是用KingGlory下的
//	//但是 编译器又有就近原则
//	//二义性
//	using KingGlory::sunwukongId;
//	cout << sunwukongId << endl;
//}
void test02()
{
	int sunwukongId = 30;
	//using编译指令
	using namespace KingGlory; //打开房间
	cout << sunwukongId << endl;
}

int main()
{
	test02();
	system("pause");
	return EXIT_FAILURE;
}

在这里插入图片描述
二义性问题

#include <iostream> 
using namespace std;

namespace KingGlory
{
	int sunwukongId = 10;
}

//void test01()
//{
//	int sunwukongId = 20;
//	//using 声明
//	//写了using声明后,下面这段diam说明以后看到的sunwukongId是用KingGlory下的
//	//但是 编译器又有就近原则
//	//二义性
//	using KingGlory::sunwukongId;
//	cout << sunwukongId << endl;
//}

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

int main()
{
	test02();
	system("pause");
	return EXIT_FAILURE;
}

在这里插入图片描述

05 C++对C语言的增强

1.全局变量检测增强
C代码:

//1.全局变量检测增强
int a;
int a = 10;

C++代码:

//1.全局变量检测增强
//int a;
int a = 10;

2.函数检测增强,参数类型增强,返回值检测增强
C代码:

//2.函数检测增强
int getRectS(w, h)
{
}
void test02()
{
	getRectS(10, 10,10);
}

C++代码:

//2.函数检测增强,参数类型增强,返回值检测增强
int getRectS(int w, int h)
{
	return w * h;
}
void test02()
{
	getRectS(10, 10); //函数调用参数检测增强
}

3.类型转换检测增强
C代码:

void test03()
{
	char* p = malloc(sizeof(64)); //malloc返回值是void*
}

C++代码:

//3.类型转换检测增强
void test03()
{
	char* p =(char*) malloc(sizeof(64)); //malloc返回值是void*
}

4.struct增强
C代码:

//4.struct增强
struct Person
{
	int m_Age;
	//void plusAge(); //c语言中struct不可以加函数
};
void test04()
{
	struct Person p1; //使用时候必须加入struct关键字
}

C++代码:

/4.struct增强
struct Person
{
	int m_Age;
	void plusAge() { m_Age++; }; //c++语言中struct可以加函数
};
void test04()
{
	Person p1; //使用时候不加struct关键字
	p1.m_Age = 10;
	p1.plusAge();
	cout << p1.m_Age << endl;
}

5.bool类型增强 C语言中没有bool类型
C代码:

// 5.bool类型增强 C语言中没有bool类型
//bool flag;

C++代码:

/ 5.bool类型增强 C语言中没有bool类型
bool flag = true;//只有真或假, true代表 真(非0)  false 代表假(0)
void test05()
{
	cout << sizeof(bool) << endl;

	flag = 100;
	//bool类型 非0的值 转为1,0就转为0
	cout << flag << endl;
}

6.三目运算符增强
C代码:

//6.三目运算符增强
void test06()
{
	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++代码:

//6.三目运算符增强
void test06()
{
	int a = 10;
	int b = 20;

	cout << "ret=" << (a > b ? a : b) << endl;
	a > b ? a : b = 100; //b=100 C++中返回的是变量
	cout << "a=" << a << endl;
	cout << "b=" << b << endl;
}

7.const增强
C代码:

//7.const增强
const int m_A = 10; //受到保护,不可以改
void test07()
{
	//m_A = 10;
	const int m_B = 20;//伪常量
	//m_B = 100;
	int* p = (int*)&m_B;
	*p = 200;
	printf("*p=%d, m_B=%d\n", *p, m_B);  //200  200

	//int arr[m_B];不可以初始化数组
}

C++代码:

//7.const增强
const int m_A = 10; //受到保护,不可以改
void test07()
{
	//m_A = 10;
	const int m_B = 20;//真正常量
	//m_B = 100;
	int* p = (int*)&m_B;
	*p = 200;
	cout << "*p=" << *p << endl; //200
	cout << "m_B=" << m_B << endl;//20

	int arr[m_B];//可以初始化数组
}

在这里插入图片描述

06 C语言中const默认外部链接,C++默认内部链接

C语言中const默认外部链接
test.c:

const int a = 10;//C语言中默认const是外部链接

C语言中的const默认是外部链接.c:

#include <stdio.h> 
#include <string.h> 
#include <stdlib.h> 

int main()
{
	extern const int a;
	printf("a=%d\n", a);//告诉编译器a在外部
	system("pause");
	return EXIT_FAILURE;
}

在这里插入图片描述
C++默认是内部链接
test.cpp:

extern const int a = 10;//C++语言中默认const是内部链接,extern提高作用域

C++语言中的const默认是内部链接.cpp:

#include <iostream>
using namespace std;


int main()
{
	extern const int a;
	cout << a << endl;
	system("pause");
	return EXIT_FAILURE;
}

在这里插入图片描述

07 const分配内存情况

1.const分配内存,取地址会分配临时内存

1.const分配内存,取地址会分配临时内存

void test01()
{
	const int m_A = 10;//符号表
	int* p = (int*)&m_A;//会分配临时内存
}

2.extern 编译器也会给const变量分配内存

2.extern 编译器也会给const变量分配内存

3.用普通变量初始化const变量

3.用普通变量初始化const变量

void test02()
{
	int a = 10;
	const int b = a;//会分配内存
	
	int* p = (int*)&b;
	*p = 1000;
	cout << "b=" << b << endl;  //b=1000
}

4.自定义数据类型,加const也会分配内存

4.自定义数据类型,加const也会分配内存

struct Person
{
	string m_Name;//姓名
	int m_Age;
};
void test03()
{
	const Person p1;
	//p1.m_Name = "aaaa";报错
	Person* p = (Person*)&p1;
	p->m_Name = "李三";
	(*p).m_Age = 18;

	cout << "姓名:" << p1.m_Name << "年龄:" << p1.m_Age << endl;
	//姓名:李三 年龄:18
}

08 尽量用const代替define

const和#define区别总结:
1.const有类型,可进行编译器类型安全检查。#define无类型,不可进行类型检查。
2.const有作用域,而#define不重视作用域,默认定义处到文件结尾,如果定义在指定作用域下有效的常量,那么#define就可能用。

09 引用的基本语法及注意事项

在这里插入图片描述

1.引用基本语法 Type &别名 = 原名

#include <iostream>
using namespace std;

//1.引用基本语法  Type &别名 = 原名
void test01()
{
	int a = 10;
	int& b = a;
	b = 20;
	cout << "a=" << a << endl;
	cout << "b=" << b << endl;
}
int main()
{
	test01();
	system("pause");
	return EXIT_FAILURE;
}

在这里插入图片描述

2.引用必须初始化

//2.引用必须初始化
void test02()
{
	//int& a;//必须初始化 报错
	int a = 10;
	int& b = a;//引用初始化后不可以修改了
	int c = 20;
	b = c;//赋值
}

3.对数组建立引用

3.对数组建立引用

//3.对数组建立引用
void test03()
{
	int arr[10];
	for (int i = 0; i < 10; i++)
	{
		arr[i] = i;
	}
	//给数组其别名
	int(&pArr)[10] = arr;
	for (int i = 0; i < 10; i++)
	{
		cout << pArr[i] << "";
	}
	cout << endl;

	//第二种方式  起别名
	typedef int(ARRAYREF)[10];//一个具有10个元素的int类型的数组
	ARRAYREF& pArr2 = arr;
	for (int i = 0; i < 10; i++)
	{
		cout << pArr2[i] << "";
	}
	cout << endl;
}
int main()
{
	test03();
	system("pause");
	return EXIT_FAILURE;
}

在这里插入图片描述

10 参数的传递方式

#include <iostream>
using namespace std;

void mySwap1(int a, int b)
{
	int tmp = a;
	a = b;
	b = tmp;
	cout << "a=" << a << endl;
	cout << "b=" << b << endl;
}

void test01()
{
	int a = 10;
	int b = 20;
	mySwap1(a, b);//值传递
	cout << "a=" << a << endl;
	cout << "b=" << b << endl;
}

//地址传递
void mySwap2(int* a, int* b)
{
	int tmp = *a;
	*a = *b;
	*b = tmp;
	cout << "*a=" << *a << endl;
	cout << "*b=" << *b << endl;
}

void test02()
{
	int a = 10;
	int b = 20;
	mySwap2(&a, &b);
	cout << "a=" << a << endl;
	cout << "b=" << b << endl;
}

//引用传递  类似传地址
void mySwap3(int &a, int &b) //&a=a &b=b
{
	int tmp = a;
	a = b;
	b = tmp;
	cout << "a=" << a << endl;
	cout << "b=" << b << endl;
}

void test03()
{
	int a = 10;
	int b = 20;
	mySwap3(a, b);
	cout << "a=" << a << endl;
	cout << "b=" << b << endl;
}

int main()
{
	test01();
	test02();
	test03();
	system("pause");
	return EXIT_FAILURE;
}

在这里插入图片描述

11 引用的注意事项

1.引用必须引用一块合法的内存空间
2.不要返回局部变量的引用

//引用的注意事项
//1.引用必须引用一块合法的内存空间
//2.不要返回局部变量的引用
int& doWork()
{
	int a = 10;
	return a;
}

void test04()
{
	//int& a = 10;//引用必须引用一块合法的内存空间 会报错
	int& ret = doWork();
	cout << "ret=" << ret << endl; //第一次10是编译器做了优化
	cout << "ret=" << ret << endl;
	cout << "ret=" << ret << endl;
	cout << "ret=" << ret << endl;
}

int& doWork2()
{
	static int a = 10;
	return a;
}
void test05()
{
	int& ret = doWork2();
	//如果函数的返回值是引用,那么这个函数调用可以作为函数的左值
	doWork2() = 1000;//相当于写了a=1000;
}

int main()
{
	test04();
	system("pause");
	return EXIT_FAILURE;
}

在这里插入图片描述
引用的本质:指针常量

12 指针的引用

#include <iostream>
using namespace std;

struct Person
{
	int m_Age;
};

void allocatMemory(Person** p) //**p 具体的Person对象 *p对象的指针  p指针的指针
{
	*p = (Person*)malloc(sizeof(Person));
	(*p)->m_Age = 100;
}
void test01()
{
	Person* p = NULL;
	allocatMemory(&p);
	cout << "p的年龄:" << p->m_Age << endl;
}

void allocatMemoryByRef(Person* &p) //**p 具体的Person对象 *p对象的指针  p指针的指针
{
	p = (Person*)malloc(sizeof(Person));
	p->m_Age = 100;
}
void test02()
{
	Person* p = NULL;
	allocatMemoryByRef(p);
	cout << "p的年龄:" << p->m_Age << endl;
}
int main()
{
	test01();
	test02();
	system("pause");
	return EXIT_FAILURE;
}

在这里插入图片描述

13 常量引用

#include <iostream>
using namespace std;

void test01()
{
	//int& ref = 10;//引入了不合法的内存,不可以  会报错
	const int& ref = 10;//加入const后,编译器处理方式为:int tmp=10;const int &ref = tmp;
	//ref =10;
	int* p = (int*)&ref;
	*p = 1000;
	cout << "ref=" << ref << endl;
}

//常量引用使用场景,用来修饰形参
void showValue(const int& val)
{
	//val += 1000;//如果只想显示内容,而不修改内容,那么就用const修饰这个形参
	cout << "val=" << val << endl;
}
void test02()
{
	int a = 10;
	showValue(a);
}
int main()
{
	test01();
	test02();
	system("pause");
	return EXIT_FAILURE;
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值