引用的本质
在研究引用的时候 ,可以将引用理解为 一个 常指针
在理解引用的时候, 可以将引用理解为 一个变量的别名
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
| #define _CRT_SECURE_NO_WARNINGS #include <iostream>
using namespace std;
struct TypeA { int *a; };
struct TypeB { double &a; };
struct Teacher { int id; char name[64]; };
void motifyTeacher(Teacher &t) { t.id = 100; }
void motifyA(int *const a) { *a = 100; }
void motifyB(int &a) { a = 1000; }
void test() { int value = 20; Teacher t1 = { 1, "zhangsan" };
motifyA(&value);
cout << "value = " << value << endl;
motifyB(value);
cout << "value = " << value << endl;
motifyTeacher(t1); }
void motifyAA(int *a) { *a = 100; }
void motifyBB(int &a) { a = 200; }
void test2() { int a = 10;
motifyAA(&a);
motifyBB(a); }
int main(void) { int a = 10; int b = 20;
const int c_a = 10;
int &re = a; re = b;
cout << "re = " << re << endl; cout << "a = " << a << endl; cout << "b = " << b << endl;
re = 100;
cout << "re = " << re << endl; cout << "a = " << a << endl; cout << "b = " << b << endl;
cout << "sizeof(TypeA): " << sizeof(TypeA) << endl; cout << "sizeof(TypeB):" << sizeof(TypeB) << endl;
test();
return 0; }
|