黑马:C++中的引用(89~94)

1.引用的基本作用

作用:给变量起别名

#include<iostream>
using namespace std;
#include<string>
#include<vector>
#include<deque>
#include<iterator>
#include<list>
#include<algorithm>  //标准算法头文件
#include<numeric>
#include<map>
#include<set>
#include<utility>
#include<fstream>
#include<string>
#include<ctime>
#include<stack>
#include<queue>


int main(int argc,char**argv) {  

	int a = 10;
	int& b = a;
	cout << "a= " << a << endl;
	cout << "b= " << b << endl;

	b = 100;

	cout << "a= " << a << endl;
	cout << "b= " << b << endl; //输出都是100
	
	system("pause");
	return  0;
}




2.引用注意事项

引用必须初始化

引用在初始化后不可改变

#include<iostream>
using namespace std;
#include<string>
#include<vector>
#include<deque>
#include<iterator>
#include<list>
#include<algorithm>  //标准算法头文件
#include<numeric>
#include<map>
#include<set>
#include<utility>
#include<fstream>
#include<string>
#include<ctime>
#include<stack>
#include<queue>


int main(int argc,char**argv) {  

	int a = 10;
	// int& b; 错误 必须初始化
	int& b = a;
	 
	int c = 20;

	// int& b = c; 错误 初始化后不可以更改

	cout << "a= " << a << endl;
	cout << "b= " << b << endl;
	cout << "c= " << c << endl;
	
	system("pause");
	return  0;
}




3.引用做函数参数

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

优点:简化指针修改实参

#include<iostream>
using namespace std;
#include<string>
#include<vector>
#include<deque>
#include<iterator>
#include<list>
#include<algorithm>  //标准算法头文件
#include<numeric>
#include<map>
#include<set>
#include<utility>
#include<fstream>
#include<string>
#include<ctime>
#include<stack>
#include<queue>

//值传递 形参不会修饰实参
void Swap(int a,int b) {
	int temp = a;
	a = b;
	b = temp;

	cout << "值传递中的a= " << a << endl;
	cout << "值传递中的b= " << b << endl;
}

//地址传递
void Swap01(int* a, int* b) {
	int temp = *a;
	*a = *b;
	*b = temp;

	cout << "地址传递中的a= " << *a << endl;
	cout << "地址传递中的b= " << *b << endl;
}

//引用传递
void Swap02(int& a, int& b) {
	int temp = a;
	a = b;
	b = temp;

	cout << "引用传递中的a= " << a << endl;
	cout << "引用传递中的b= " << b << endl;
}

int main(int argc,char**argv) {  

	int a = 10;
	int b = 20;
	//Swap(a, b);
	//Swap01(&a, &b);
	Swap02(a, b);


	cout << "a= " << a << endl;
	cout << "b= " << b << endl;
	
	
	system("pause");
	return  0;
}




4.引用做函数返回值

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

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

用法:函数调用作为左值

#include<iostream>
using namespace std;
#include<string>
#include<vector>
#include<deque>
#include<iterator>
#include<list>
#include<algorithm>  //标准算法头文件
#include<numeric>
#include<map>
#include<set>
#include<utility>
#include<fstream>
#include<string>
#include<ctime>
#include<stack>
#include<queue>

//1.不要返回局部变量的引用
int& test01() {
	int a = 10; //存放在栈区
	return a;
}

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


int main(int argc,char**argv) {  

	//int& ref = test01();
	//cout << "ref = " << ref << endl;   第一次可以是因为编译器做了保留
	//cout << "ref = " << ref << endl;   a的内存已经被释放了
	
	int& ref2 = test02();
	cout << "ref2 = " << ref2 << endl;
	cout << "ref2 = " << ref2 << endl;

	test02() = 1000; //如果函数的返回值是引用,这个函数调用可以作为左值

	cout << "ref2 = " << ref2 << endl;
	cout << "ref2 = " << ref2 << endl;
	system("pause");
	return  0;
}




5.引用的本质

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

 

推荐使用引用技术,语法方便,引用本质是指针常量,但是指针的操作编译器都帮我们做了

6.常量引用

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

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

#include<iostream>
using namespace std;
#include<string>
#include<vector>
#include<deque>
#include<iterator>
#include<list>
#include<algorithm>  //标准算法头文件
#include<numeric>
#include<map>
#include<set>
#include<utility>
#include<fstream>
#include<string>
#include<ctime>
#include<stack>
#include<queue>

void show(const int& val) {
	// val = 1000; 报错,防止误操作,通过形参修改实参
	cout << "val= " << val <<endl;
}

int main(int argc,char**argv) {  


	//常量引用:
	
	//int &ref=10 报错 必须初始化
	// const int& ref = 10; 正确 加了const相当于编译器将代码修改为 int temp=10; const int &ref=temp;
	// ref = 20; 加了const之后变为只读,不可以修改

	int a = 10;
	show(a);

	system("pause");
	return  0;
}




 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值