引用的基本使用
**作用: **给变量起别名
语法: 数据类型 &别名 = 原名
1 #include <iostream> 2 using namespace std; 3 int main() 4 { 5 int a = 10; 6 int* b = &a; 7 int& c = a; 8 cout << "&a :" << &a << " " << "\t a :" << a << endl; 9 cout << " b :" << b << " " << "\t*b :" << *b << " " << "\t&b :" << &b << endl; 10 cout << "&c :" << &c << " " << "\t c :" << c << endl; 11 c = 20; 12 cout << endl; 13 cout << "&a :" << &a << " " << "\t a :" << a << endl; 14 cout << " b :" << b << " " << "\t*b :" << *b << " " << "\t&b :" << &b << endl; 15 cout << "&c :" << &c << " " << "\t c :" << c << endl; 16 return 0; 17 }
引用的注意事项 :
- 引用必须初始化
- 引用在初始化后,不可以改变
1 #include <iostream> 2 using namespace std; 3 int main() 4 { 5 int a = 20; 6 //“int& b”这种写法会报错:b必须要初始值设定 7 int& b = a;//b 的初始化 8 int c = 10; 9 // int& b = c; 一旦初始化后不能更改,要不然会报错 :"b":重定义;多次初始化 10 b = c;//这是赋值操作不是更改引用 11 }
引用做函数参数 :
值传递,地址传递,引用做函数参数三者对比
值引用 :
1 #include <iostream> 2 using namespace std; 3 void chan1(int a, int b)//值传递,赋值操作 4 { 5 int temp = a; 6 a = b; 7 b = temp; 8 cout << "chan1a :" << a << endl 9 << "chan1b :" << b << endl; 10 } 11 int main() 12 { 13 int a = 10; 14 int b = 20; 15 chan1(a, b); 16 cout << "a :" << a <<endl 17 << "b :" << b; 18 return 0; 19 }
地址传递:
1 #include <iostream> 2 using namespace std; 3 void chan1(int *a, int *b)//地址传递,指针操作 4 { 5 int temp = *a; 6 *a = *b; 7 *b = temp; 8 cout << "chan1*a :" << *a << endl 9 << "chan1*b :" << *b << endl; 10 } 11 int main() 12 { 13 int a = 10; 14 int b = 20; 15 chan1(&a, &b); 16 cout << "a :" << a <<endl 17 << "b :" << b; 18 return 0; 19 }
引用做函数参数
**作用:**函数传参时,可以利用引用的技术让形参修饰实参
**优点:**可以简化指针修改实参
:
1 #include <iostream> 2 using namespace std; 3 void chan1(int &c, int &d)//引用做函数参数 4 { 5 int temp = c; 6 c = d; 7 d = temp; 8 cout << "chan1 c :" << c << endl 9 << "chan1 d :" << d << endl; 10 } 11 int main() 12 { 13 int a = 10; 14 int b = 20; 15 chan1(a, b); 16 cout << "a :" << a <<endl 17 << "b :" << b; 18 return 0; 19 }
总结:通过引用参数产生的效果同按地址传递是一样的。引用的语法更清楚简单
引用做函数返回值:
//本质是& ref() =返回值 a
1 #include <iostream> 2 using namespace std; 3 int & ref() 4 { 5 int a = 10;//把a放在了栈区,函数运行结束后会自动释放a的值使得第二次输出时输出乱码 6 return a; 7 } 8 int main() 9 { 10 int& b = ref(); 11 cout << "b:" << b << endl; 12 cout << "b:"<< b << endl; 13 14 }
1 #include <iostream> 2 using namespace std; 3 int & ref() 4 { 5 static int a = 10;//static 使得a成为了静态变量,在全局区,只会在程序结束时释放或者人为释放 6 return a; 7 } 8 int main() 9 { 10 int& b = ref(); 11 cout << "b:" << b << endl; 12 cout << "b:"<< b << endl; 13 14 }
1 #include <iostream> 2 using namespace std; 3 int & ref() 4 { 5 static int a = 10;//static 使得a成为了静态变量,在全局区,只会在程序结束时释放或者人为释放 6 return a; 7 } 8 int main() 9 { 10 int& b = ref(); 11 cout << "b:" << b << endl; 12 cout << "b:"<< b << endl; 13 ref() = 100;//顺序是&ref()= a ,此时ref()就是a的别名,&b = ref(),因为ref()是a的别名,所以可以被赋值 14 cout << "b:" << b << endl; 15 cout << "b:" << b << endl; 16 cout << "ref():" << ref() << endl; 17 cout << "ref():" << ref() << endl; 18 return 0; 19 }
引用的本质
本质:引用的本质在c++内部实现是一个指针常量.
1 #include <iostream> 2 using namespace std; 3 int *const ref()//指针常量,可以改变指向地址的函数的值,不能改变指向的地址 4 { 5 static int a = 10; 6 return &a; 7 } 8 int main() 9 { 10 cout << "ref():" << ref() << endl; 11 int*const b = ref();//指针常量,可以改变指向地址的函数的值,不能改变指向的地址 12 cout << "*b:" << *b << endl; 13 cout << "*b:"<< *b << endl; 14 *ref() = 100;//因为函数返回值是a的引用,也就是说返回的是函数a,所以可以赋值;而函数定义时不加引用,那么返回的是值a 15 cout << "*b:" << *b << endl; 16 cout << "*b:" << *b << endl; 17 cout << "*ref():" << *ref() << endl; 18 cout << "*ref():" << *ref() << endl; 19 return 0; 20 }
结论:C++推荐用引用技术,因为语法方便,引用本质是指针常量,但是所有的指针操作编译器都帮我们做了
const 修饰引用(也叫常量引用):
**作用:**常量引用主要用来修饰形参,防止误操作
在函数形参列表中,可以加==const修饰形参==,防止形参改变实参
1 #include <iostream> 2 using namespace std; 3 void refu(const int& c)//本质是 const int * const c 4 { 5 //c = 1000;因为被const修饰了所以值不能改变 6 cout << c << endl; 7 } 8 int main() 9 { 10 int a = 10; 11 //int& b = 20;错误操作引用必须是合法的内存空间,而20是在系统保留的内存空间里 12 int& b = a; 13 refu(a); 14 cout << a << endl; 15 return 0; 16 }
意义在于简化了、语法
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!