c++引用

2020-12-13 14:45

阅读:427

标签:初始化   左值   方式   using   ret   必须   space   别名   技术   

首先我们知道任何变量都有左值和右值,分别对应变量的内存区域和内存区域中的值

引用其实就是变量的别名,其实是一个东西有两个名吧

定义方式 类型 &引用名 = 变量名 而且必须定义的时候就初始化,不然是错误的

看个例子吧

 1 #include 2 using namespace std;
 3 
 4 int main()
 5 {
 6     int i = 9;
 7     int *p = &i;
 8     int &ir = i;
 9     cout " " " "  endl;
10     
11     cout " " " "  endl;
12     cout " " " "  endl;
13     
14     *p = 3;
15     cout " " " "  endl;
16     cout " " " "  endl;
17     
18     ir = 8;
19     cout " " " "  endl;
20     cout " " " "  endl;
21     
22     i = 99;
23     cout " " " "  endl;
24     cout " " " "  endl;
25     
26     int *p2 = &ir;
27     cout " " " " " "  endl;
28     
29     *p2 = 666;
30     cout " " " " " "  endl;
31     cout " " " " " "  endl;
32     cout " " " " " "  endl;
33     return 0;
34 } 

技术图片

 

c++引用

标签:初始化   左值   方式   using   ret   必须   space   别名   技术   

原文地址:https://www.cnblogs.com/mch5201314/p/11568699.html


评论


亲,登录后才可以留言!