23 一般引用的用法

(1)代码

 1 #include <iostream>
 2 #include <string>
 3 using namespace std;
 4 int main(int argc, char* argv[])
 5 {
 6     int a = 10;
 7     int b = 20;
 8     int &rn = a;//声明rn为变量a的引用
 9     int equal;
10 
11     rn = b;//rn是a的别名 也就是b赋值给rn实际上就是修改了a的值 引用不用开辟内存单元
12     cout << "a=" << a << endl;//10
13     cout << "b=" << b << endl;//20
14 
15     rn = 100;
16     cout << "a=" << a << endl;//100
17     cout << "b=" << b<< endl;//20
18 
19     equal = (&a == &rn) ? 1 : 0;
20     cout << "equal=" << equal << endl;//1
21 
22     getchar();
23 
24     return 0;
25 
26 }
View Code

相关文章:

  • 2021-12-15
  • 2022-12-23
  • 2022-12-23
  • 2021-04-08
  • 2021-12-01
猜你喜欢
  • 2021-10-06
  • 2022-01-23
  • 2021-05-23
  • 2021-06-08
  • 2021-09-29
  • 2021-12-28
相关资源
相似解决方案