1、引用的基本语法
cpp#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、引用的注意事项
cpp#include <iostream> using namespace std; int main() { int a = 10; //1、引用必须初始化 //int& b; 错,必须初始化 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、引用做函数参数
cpp#include <iostream> using namespace std; //交换函数 //1、值传递 void mySwap01(int a, int b) { int temp = a; a = b; b = temp; /*cout << "swap01 a = " << a << endl; cout << "swap01 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 temp = a; a = b; b = temp; } int main() { int a = 10; int b = 20; //mySwap01(a, b);//值传递,形参不会修饰实参 //mySwap02(&a, &b);地址传递,形参会修饰实参 mySwap03(a, b);//引用传递,形参会修饰实参 cout << "a = " << a << endl; cout << "b = " << b << endl; system("pause"); return 0; }
4、引用做函数的返回值
cpp#include <iostream> using namespace std; //引用做函数的返回值 //1、不要返回局部变量的引用 int& test01() { int a = 10;//局部变量存放在四区中的栈区 return a; } //2、函数的调用可以作为左值 int& test02() { static int a = 10;//静态变量 存放在全局区,全局区的数据存放在程序结束后系统释放 return a; } int main() { //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、引用的本质
引用的本质是指针常量
cpp#include <iostream> using namespace std; void func(int& ref) { ref = 100;//red是引用,转化为*ref=100 } int main() { int a = 10; //自动转换成 int* const ref=&a; 指针常量是指针指向不可改变, int& ref = a; ref = 20;//内部发现ref是引用,自动帮我们转换为:*ref=20; cout << "a:" << a << endl; cout << "ref:" << ref << endl; func(a); system("pause"); return 0; }
6、常量引用
cpp#include <iostream> using namespace std; //打印数据函数 void showValue(const int& val) { //val = 1000; cout << "val = " << val << endl; } int main() { //常量引用 //使用场景:用来修饰形参,防止误操作 int a = 100; //int& ref = 10;引用必须引一块合肥的内存空间 加上const之后 编译器将代码修改 int temp=10; const int& ref=temp; //const int& ref = 10; //ref = 20;//报错,加入const之后变为只读,不可以修改 showValue(a); cout << "a = " << a << endl; system("pause"); return 0; }
引用(个人学习笔记黑马学习)
寻心的无心者2023-09-01 22:14