Java教程

QT乱翻书-引用

本文主要是介绍QT乱翻书-引用,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

跳转到大纲

 

QT乱翻书-引用

  1 #include <iostream>
  2 
  3 using namespace std;
  4 /*
  5 引用  给函数传地址
  6 1、引用的本质,给变量名取别名,常量指针。
  7 int a = 10;
  8 int &b = a; int * const b = &a;
  9 2、引用作为函数的参数,函数内部可以通过引用操作外部变量
 10 节约空间
 11 3、引用作为函数的返回值类型,返回值必须被static
 12 4、常引用作为函数参数,防止函数内部修改参数的值。
 13 */
 14 
 15 //给普通变量取别名
 16 void test01()
 17 {
 18     int a = 100;
 19     int &b  = a;//引用必须初始化
 20     cout << "b = " << b << endl;
 21     a = 300;
 22     cout << "b = " << b << endl;
 23     cout << "&a = " << &a << endl;
 24     cout << "&b = " << &b << endl;
 25 }
 26 
 27 
 28 //给数组取别名
 29 void test02()
 30 {
 31     int arr[5] = {10, 20, 30, 40, 50};
 32     int n = sizeof(arr) / sizeof(arr[0]);
 33     for(int i = 0; i < n; i++) {
 34         cout << arr[i] << " ";
 35     }
 36     cout << endl;
 37     int (&myarr)[5] = arr; //int (*p)[5] = &arr;
 38     for(int i = 0; i < n; i++) {
 39         cout << myarr[i] << " ";
 40     }
 41     cout << endl;
 42 }
 43 
 44 //给指针变量取别名
 45 void test03()
 46 {
 47     int num = 100;
 48     int *p = &num;
 49     int * &myp = p;
 50     cout << "*p = " << *p << endl;
 51     cout << "*&myp = " << *myp << endl;
 52 }
 53 
 54 //给函数取别名
 55 void fun01()
 56 {
 57     cout << "fun01" << endl;
 58 }
 59 void test04()
 60 {
 61     void (&my_fun)() = fun01;
 62     my_fun();
 63 }
 64 // 引用只一次
 65 void test05()
 66 {
 67     int a = 10;
 68     int b = 20;
 69     int &num = a;
 70     num = b;
 71     cout << "a = " << a << endl;
 72     cout << "num = " << num << endl;
 73     cout << "b = " << b << endl;
 74 }
 75 
 76 //引用作为函数参数
 77 void swap01(int *p1, int *p2)
 78 {
 79     int tmp = *p1;
 80     *p1 = *p2;
 81     *p2 = tmp;
 82 }
 83 
 84 void swap02(int &x, int &y) // int &x = a;
 85 {
 86     int tmp = x;
 87     x = y;
 88     y = tmp;
 89 }
 90 
 91 
 92 
 93 void test06()
 94 {
 95     int a = 10;
 96     int b = 20;
 97     cout << "a = " << a << "," << "b = " << b << endl;
 98 //    swap01(&a, &b);
 99     swap02(a, b);
100     cout << "a = " << a << "," << "b = " << b << endl;
101 }
102 
103 //引用作为函数返回值
104 int & getData(void)
105 {
106     static int num = 100;
107     return num; // 不要返回局部变量的引用。
108 }
109 void test08()
110 {
111     int &b = getData();
112     cout << "b = " << b << endl;
113 }
114 
115 //链式函数
116 struct stu {
117     stu& printStu(stu &ob, int value)
118     {
119         cout << value << " "  ;
120         return ob;
121     }
122 };
123 
124 void test09()
125 {
126     stu ob1;
127     ob1.printStu(ob1, 100).printStu(ob1, 200).printStu(ob1, 300);
128     cout << endl;
129 }
130 
131 //常量取别名
132 void test10()
133 {
134     const int & a = 10;//a 就是10的别名
135 //    a = 100; //err
136     //常引用 不能通过常引用修改内容。
137     //常引用作为函数参数,防止函数内部修改参数的值
138     cout << a << endl;
139 }
140 
141 void printInt01(int &a)
142 {
143     a = 300;
144     cout << "a=" << a << endl;
145 }
146 void printInt02(const int &a)
147 {
148 //    a = 300;//err
149     cout << "a=" << a << endl;
150 }
151 void test11()
152 {
153     int num = 100;
154     printInt01(num);
155     num = 200;
156     printInt02(num);
157 }
158 int main()
159 {
160     test01();
161     test02();
162     test03();
163     test04();
164     test05();
165     test06();
166     test08();
167     test09();
168     test10();
169     test11();
170     return 0;
171 }

 

这篇关于QT乱翻书-引用的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!