大约耗时1分钟

引用如何使用

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

  1. int main(){
  2. int a = 10;
  3. int &b = a;
  4. cout << "a = " << a << endl;
  5. cout << "b = " << b <<endl;
  6. b = 6666;
  7. cout <<"a = " << a << endl;
  8. cout << "b = " << b << endl;
  9. }
  10. 输出结果:
  11. a = 10
  12. b = 10
  13. a = 6666
  14. b = 6666

其实它的本质就是给变量起别名

引用的注意点

  1. int main(){
  2. int &b;//报错 未初始化 error: 'b' declared as reference but not initialized
  3. int a = 10;
  4. int &c = a;//正确
  5. int d = 20;
  6. c = d; //这个是赋值操作 不是更改引用
  7. return 0;
  8. }

所以引用必须初始化、引用初始化后,不可以改变,不会改变

引用作为函数形参

  1. #include<iostream>
  2. #include<string.h>
  3. using namespace std;
  4. //值传递
  5. void swap01(int a, int b){
  6. int temp = a;
  7. a = b;
  8. b = temp;
  9. }
  10. //地址传递
  11. void swap02(int *a, int *b){
  12. int temp = *a;
  13. *a = *b;
  14. *b = temp;
  15. }
  16. //引用传递 这里 a 和 b 就是实参的别名
  17. void swap03(int &a, int &b){
  18. int temp = a;
  19. a = b;
  20. b = temp;
  21. }
  22. int main(){
  23. int a = 10;
  24. int b = 20;
  25. swap03(a, b);
  26. cout << "a = " << a << endl;
  27. cout << "b = " << b << endl;
  28. return 0;
  29. }
  30. 输出结果:
  31. a = 20
  32. b = 10

值传递,只是形参的值会替换,实际值不会变
地址传递,都会改变
引用传递,都会改变

引用作为函数返回值

  1. #include<iostream>
  2. #include<string.h>
  3. using namespace std;
  4. //返回局部变量引用
  5. int& test01(){
  6. //局部变量存在栈区
  7. int a = 10;
  8. return a;
  9. }
  10. //返回静态变量引用
  11. int& test02(){
  12. //静态变量存在全局区
  13. static int a = 20;
  14. return a;
  15. }
  16. int main(){
  17. int &ref = test02();
  18. cout << "ref = " << ref << endl;
  19. test02() = 66666; // test02 返回的变量static int a 和 ref 是接收 test02返回的别名 操作结果是一样的
  20. cout << "after ref = " << ref << endl;
  21. return 0;
  22. }
  23. 输出结果:
  24. warning: reference to local variable 'a' returned [-Wreturn-local-addr]
  25. int a = 10;
  26. ^
  27. ref = 20
  28. after ref = 66666

出现的waring 是因为test01方法返回了栈区的变量引用,这个在方法调用完就会被释放的,程序检查出来了。如果你要运行这个方法,那么要么报错,要么卡死在这

引用的本质

  1. void func(int& ref){
  2. ref = 100; //等价于 *ref = 100;
  3. }
  4. int main(){
  5. int a = 10;
  6. //自动转换为 int* const ref = &a;指针常量 指向不可修改,即引用不能修改
  7. int& ref = a;
  8. ref = 20; // 内部发现是引用,会自动转换成 *ref = 20
  9. cout << "a : " << a << endl;
  10. cout << "ref :" << ref <<endl;
  11. func(a);
  12. return 0;
  13. }

int & ref = a 等价于 int const ref = &a 常量指针
*所以引用本质其实就是一个指针常量

常量引用

常量引用最重要的作用就是来修饰形参,防止函数体内修改到实参

#include<iostream>
#include<string.h>
using namespace std;

void showValue(const int & v){
    v+=100;//注意这里 修改了常量引用的值
    cout << "showValue v = " << v << endl;
}
int main(){
    int a = 10;
    int& ref = a;
    cout << "ref = " << ref << endl;
    showValue(ref);
    cout << "after showValue ref = " << ref << endl;
}
执行报错
error: assignment of read-only reference 'v'
     v+=100;

常量引用的作用相信你已经ok了。但是你还有可能会问,我引用为什么要这样写 int& ref = a ,为什么不能直接int& ref = 10,首先我们引用是给变量起别名,你是变量么?这么写,而且引用本身需要一个合法的内存空间,毕竟引用就是一个常量指针,我指向的内存地址肯定要是合法的。
但是 const int & ref = 10 写法是对的。加了const 编译器会将代码修改为如下 int temp =10, const int& ref = temp,但依旧是不能修改的。