目录
1.引用的基本使用
#include<iostream>
using namespace std;
int main()
{
//引用的基本语法
// 引用的本质给变量起别名
//数据类型 &别名 = 原名,数据类型与原名的数据类型一样
//别名,原名操纵的是同一块内存
//不管是用别名操纵,还是原名操纵,都是修改的同一块内存
int a = 10;
//创建引用
int& b = a;
cout << "a = " << a << endl;
cout << "b= " << b << endl;
b = 100;
cout << "a = " << a << endl;
cout << "b= " << b << endl;
system("pause");//按任意键继续
return 0;
}
2.引用注意事项
#include<iostream>
using namespace std;
int main()
{
//引用的注意事项
//1、引用必须初始化
int a = 10;
int& b = a;
//2、引用初始化后不可以更改
int c = 20;
b = c;//赋值操作,而不是更改引用
cout << "a = " << a << endl;
cout << "b = " << b << endl;
cout << "c = " << c << endl;
system("pause");//按任意键继续
return 0;
}
3.引用做函数参数
#include<iostream>
using namespace std;
//交换函数
//1、值传递
void swap01(int a, int b)
{
int temp;
temp = a;
a = b;
b = temp;
cout << "swap01中:" << "a = " << a << " " << "b = " << b << endl;
}
//2、地址传递
void swap02(int* p1, int* p2)
{
int temp;
temp = *p1;
*p1 = *p2;
*p2 = temp;
cout << "swap02中:" << "a = " << *p1 << " " << "b = " << *p2 << endl;
}
//3.引用传递
void swap03(int& a, int& b)
{
int temp = a;
a = b;
b = temp;
cout << "swap03中:" << "a = " << a << " " << "b = " << b << endl;
}
int main()
{
//
int a = 10, b = 20;
swap01(a, b);//值传递,形参改变不会影响实参
cout << "a = " << a << " " << "b = " << b << endl;
swap02(&a, &b);//地址传递,形参改变会影响实参
cout << "a = " << a << " " << "b = " << b << endl;
swap03(a, b);//引用传递,形参改变会影响实参
cout << "a = " << a << " " << "b = " << b << endl;
system("pause");//按任意键继续
return 0;
}
4.引用做参数返回值、
#include<iostream>
using namespace std;
//引用做函数的返回值
//1、不要返回局部变量的引用
int& test01()
{
int a = 10;//局部变量存放在四区中的 栈区
return a;
}
//2、函数的调用可以作为左值
int& test02()
{
static int a = 10;//静态变量,存放在全局区,全局区上的数据在程序结束后由系统释放
return a;
}
//类比知识
int* fun()
{
static int a = 10;
return &a;
}
int main()
{
//1、不要返回局部变量的引用
int& ref = test01();
cout <<"ref = " << ref << endl;//第一次结果正确,是因为编译器做了保留
cout << "ref = " << ref << endl;//第一次结果错误,因为a的内存已经释放
//2、函数的调用可以作为左值
int& ref1 = test02();//ref1是a的别名
cout << "ref1 = " << ref1 << endl;
cout << "ref1 = " << ref1 << endl;
//如果函数的返回值是引用,这个函数的调用可以作为左值
test02() = 1000;//test02()返回变量a的引用,就是返回变量a,相当于a=1000
//原名赋值1000,别名也就是1000了
cout << "ref1 = " << ref1 << endl;
cout << "ref1 = " << ref1 << endl;
//类比知识
*fun() = 20;
cout << *fun() << endl;
system("pause");//按任意键继续
return 0;
}
5.引用的本质--指针常量
指针常量,指针的指向不可以修改,指针指向的值可以修改。
#include<iostream>
using namespace std;
//发现是引用,转换为int* const ref = &a;
void func(int& ref)
{
ref = 100;//ref是引用,转换为*ref=100;
}
int main()
{
int a = 10;
//自动转换为 int* ref = &a;指针常量,指针的指向不可以修改,也说明了为什么引用不可以修改
int& ref = a;
ref = 20;//内部发现ref是引用,自动帮我们转换为:*ref=20;
cout << "a = " << a << endl;//输出:20
cout << "ref :" << ref << endl;//输出:20
func(a);
cout << "a = " << a << endl;//输出:100
cout << "ref :" << ref << endl;//输出:100
//C++推荐用引用技术,因为语法方便,引用本质是指针常量,但是所有的指针操作编译器都帮我们做了
system("pause");//按任意键继续
return 0;
}
6.常量引用--顾名思义在引用前加const,左值,右值
#include<iostream>
using namespace std;
//常量引用
//使用场景:用来修饰形参防止误操作
//打印数据
void showValue(const int& val)//防止误操作
{
//val = 1000;
cout << "val = " << val << endl;
}
int main()
{
//int& ref = 10;//错误,引用必须引用栈区和堆区的数据,而10在常量区,引用是给变量起别名嘛
int a = 10;
int& reff = a;//a放在栈区
//加上const之后 编译器将代码修改 int temp=10; const int& ref=temp;
const int& ref = 10;
//ref = 20;//加入const之后变为只读,不可以修改
int a = 100;
showValue(a);
system("pause");//按任意键继续
return 0;
}
#include<iostream>
using namespace std;
void func(int& a)
{
cout << a << endl;
}
//常量引用
void func2(const int& a)
{
cout << a << endl;
}
int main()
{
int a = 10;//输出:10
func(a);
//func(5);//报错,报错原因:非常量引用的的初值必须为左值
//等号左边称为左值,右边称为右值,左值可以修改,右值不可以修改
cout << "-----------" << endl;
func2(a); //输出:10
func2(5);//输出:5
system("pause");//按任意键继续
return 0;
}