C++核心之C++中的引用P89-P94

2.1引用的基本语法

作用: 给变量起别名

语法: 数据类型 &别名 = 原名

#include<iostream>
using namespace std;

int main()
{
	//引用的基本语法
	//数据类型 &别名=原名
	int a = 10;
	int &b = a;//b就是别名

	cout << "a = " << a << endl;//10
	cout << "b = " << b << endl;//10

	b = 100;

	cout << "a = " << a << endl;//100
	cout << "b = " << b << endl;//100

	system("pause");

	return 0;
}

运行结果:

2.2引用的注意事项

  • 引用必须初始化
  • 引用在初始化后,不可以改变
#include<iostream>
using namespace std;

int main()
{
	int a = 10;
	//1.引用必须初始化
	//int &b;错误,必须要初始化
	int& b = a;

	//2.引用初始化,不可以改变
	int c = 20;
	//int& b = c;错误

	b = c;//这个不是错误的,因为这个是赋值操作,而不是更改引用

	cout << "a = " << a << endl;//20
	cout << "b = " << b << endl;//20
	cout << "c = " << c << endl;//20

	b = 100;

	cout << "a = " << a << endl;//100
	cout << "b = " << b << endl;//100
	cout << "c = " << c << endl;//20
	

	system("pause");

	return 0;
}

运行结果:

2.3引用做函数参数

作用:函数传参时,可以利用引用的技术让形参修饰实参

优点:可以简化指针修改实参

注意:调用一个函数在传入参数时两种方式:值传递和地址传递,在值传递中,不能让形参修改实参

#include<iostream>
using namespace std;

//交换函数

//1.值传递
void mySwap01(int a, int b) {
	int temp = a;
	a = b;
	b = temp;
}

//2.地址传递
void mySwap02(int* a,int* b)
{
	int temp = *a;
	* a = * b;
	*b = temp;
}

//3.引用传递
void mySwap03(int &a,int &b)
{
	int temp = a;
	a = b;
	b = temp;
}

int main()
{
	int a = 10;
	int b = 20;

	mySwap01(a, b);
	cout << "swap01 a=" << a << endl;//10
	cout << "swap01 b=" << b << endl;//20   值传递,实参没有被改变

	mySwap02(&a, &b);
	cout << "swap02 a=" << a << endl;//20
	cout << "swap02 b=" << b << endl;//10   实参被改变了,因为在地址传递中,形参可以修改实参

	int c = 10;
	int d = 20;
	mySwap03(c,d);//引用传递,形参也可以修改实参   上面函数中的a,b是这里c,d的别名
	cout << "swap03 a=" << c << endl;//20
	cout << "swap03 b=" << d << endl;//10

	system("pause");

	return 0;
}

运行结果:

总结: 通过引用参数产生的效果同按地址传递是一样的。引用的语法更清楚简单。

2.4引用做函数返回值

作用:引用是可以作为函数的返回值存在的

注意:不要返回局部变量引用

用法:函数调用作为左值

#include<iostream>
using namespace std;

//引用做函数的返回值
//1.不要返回局部变量的引用
int& test01()
{
	int a = 10;//局部变量存放在四区中的栈区
	return a;//其实返回的是a的引用
}

//2.函数的调用可以作为左值
int& test02()
{
	static int a = 10;//静态变量,存放在全局区,全局区上的数据在程序结束后由系统释放
	return a;
}


int main()
{
	int &ref1 = test01();//这里可以认为ref1是a的别名

	cout << "ref1=" << ref1 << endl;//第一次结果正确,是因为编译器做了保留
	cout << "ref1=" << ref1<< endl;//第二次结果错误,是因为a的内存已经释放

	int &ref2 = test02();
	cout << "ref2=" << ref2 << endl;//10
	cout << "ref2=" << ref2 << endl;//10

	test02() = 100;//如果函数的返回值是引用,那这个函数调用可以作为左值。原名赋值100,再去访问别名ref2
	cout << "ref2=" << ref2 << endl;//100
	cout << "ref2=" << ref2 << endl;//100

	system("pause");

	return 0;
}

运行结果:

2.5引用的本质

本质:引用的本质在c++内部实现是一个指针常量

#include<iostream>
using namespace std;

void function(int& ref)
{
	ref = 100;//ref是引用,转换为*ref=100
}

int main()
{
	int a = 10;
	
	//自动转换成int* const ref=&a;即ref里面放的是a的地址  指针向量的指针指向是不可更改的,这也说明了为什么引用也不可更改
	int& ref = a;
	//ref是a的别名,内部发现ref是引用,自动转换成*ref=20;
	ref = 20;

	cout << "a=" << a << endl;//20
	cout << "ref=" << ref << endl;//20

	function(a);//发现是引用,转换为int *const ref=&a
	cout << "a=" << a << endl;//100
	cout << "ref=" << ref << endl;//100

	system("pause");

	return 0;
}

结论:C++推荐用引用技术,因为语法方便,引用本质是指针常量,但是所有的指针操作编译器都帮我们做了

2.6常量引用

作用:常量引用主要用来修饰形参,防止误操作

在函数形参列表中,可以加const修饰形参,防止形参改变实参。

#include<iostream>
using namespace std;


void showValue(const int& val) 
{
	//val = 100;
	cout <<"val="<< val << endl;
}

int main() 
{
	//常量引用
	//使用场景:用来修饰形参,加上const防止误操作

	//int& ref = 10;  引用本身需要一个合法的内存空间,因此这行错误
	//加入const就可以了,编译器优化代码,int temp = 10; const int& ref = temp;
	const int& ref = 10;
	//ref=20;错误,const在左边是定了值的 ,加入const后不可以修改变量
	cout <<"ref="<< ref << endl;

	//函数中利用常量引用防止误操作修改实参
	int a = 10;
	showValue(a);
	cout << "a=" << a << endl;

	system("pause");

	return 0;
}

运行结果:

有关const方面的理解,可以参考这篇博客:(87条消息) C++ const的用法详解_Mi ronin的博客-CSDN博客_c++ const

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值