|
4 | 4 | 2. 但是引用需要在声明的时候初始化,而且一旦指向某个对象,不会再改变; |
5 | 5 | 3. 对初始化的引用再次赋值,是对引用指向的对象进行赋值,引用本身始终指向最初的对象,它是那个对象的别名。 |
6 | 6 |
|
| 7 | +=== "snippet-1" |
7 | 8 |
|
8 | | -```cpp |
9 | | -#include <iostream> |
10 | | -void swapByPointer(int* a, int* b) { |
11 | | - int temp = *a; |
12 | | - *a = *b; |
13 | | - *b = temp; |
14 | | -} |
15 | | -void swapByReference(int& a, int& b) { |
16 | | - int temp = a; |
17 | | - a = b; |
18 | | - b = temp; |
19 | | -} |
20 | | -int main() { |
21 | | - int x = 5; |
22 | | - int y = 10; |
23 | | - std::cout << "Before swapping: x = " << x << ", y = " << y << std::endl; |
24 | | - // 使用指针交换变量的值 |
25 | | - swapByPointer(&x, &y); |
26 | | - std::cout << "After swapping by pointer: x = " << x << ", y = " << y << std::endl; |
27 | | - x = 5; |
28 | | - y = 10; |
29 | | - // 使用引用交换变量的值 |
30 | | - swapByReference(x, y); |
31 | | - std::cout << "After swapping by reference: x = " << x << ", y = " << y << std::endl; |
32 | | - return 0; |
33 | | -} |
34 | | -``` |
| 9 | + ```cpp |
| 10 | + #include <iostream> |
| 11 | + void swapByPointer(int* a, int* b) { |
| 12 | + int temp = *a; |
| 13 | + *a = *b; |
| 14 | + *b = temp; |
| 15 | + } |
| 16 | + void swapByReference(int& a, int& b) { |
| 17 | + int temp = a; |
| 18 | + a = b; |
| 19 | + b = temp; |
| 20 | + } |
| 21 | + int main() { |
| 22 | + int x = 5; |
| 23 | + int y = 10; |
| 24 | + std::cout << "Before swapping: x = " << x << ", y = " << y << std::endl; |
| 25 | + // 使用指针交换变量的值 |
| 26 | + swapByPointer(&x, &y); |
| 27 | + std::cout << "After swapping by pointer: x = " << x << ", y = " << y << std::endl; |
| 28 | + x = 5; |
| 29 | + y = 10; |
| 30 | + // 使用引用交换变量的值 |
| 31 | + swapByReference(x, y); |
| 32 | + std::cout << "After swapping by reference: x = " << x << ", y = " << y << std::endl; |
| 33 | + return 0; |
| 34 | + } |
| 35 | + ``` |
35 | 36 |
|
36 | | -```cpp |
37 | | -#include <iostream> |
38 | | -using namespace std; |
39 | | -class Parent |
40 | | -{ |
41 | | -public: |
42 | | - virtual void v_test() { cout << "Parent::v_test" << endl; } |
43 | | - int a = 0; |
44 | | -}; |
45 | | -class Child : public Parent |
46 | | -{ |
47 | | -public: |
48 | | - void v_test() override { cout << "Child::v_test" << endl; } |
49 | | -}; |
50 | | -int main() |
51 | | -{ |
52 | | - Parent parent; |
53 | | - Child child; |
54 | | - parent.a = 1; |
55 | | - child.a = 2; |
56 | | - Parent* p = &child; |
57 | | - Parent& ref_parent = parent; |
58 | | - cout << p->a << endl; |
59 | | - cout << ref_parent.a << endl; |
60 | | - // 对引用指向的对象进行赋值 |
61 | | - // 改变的是对象内部的值,a的值变了 |
62 | | - ref_parent = child; |
63 | | - cout << ref_parent.a << endl; |
64 | | - p->v_test(); |
65 | | - ref_parent.v_test(); |
66 | | - return 0; |
67 | | -} |
68 | | -``` |
| 37 | +=== "snippet-2" |
| 38 | + |
| 39 | + ```cpp |
| 40 | + #include <iostream> |
| 41 | + using namespace std; |
| 42 | + class Parent |
| 43 | + { |
| 44 | + public: |
| 45 | + virtual void v_test() { cout << "Parent::v_test" << endl; } |
| 46 | + int a = 0; |
| 47 | + }; |
| 48 | + class Child : public Parent |
| 49 | + { |
| 50 | + public: |
| 51 | + void v_test() override { cout << "Child::v_test" << endl; } |
| 52 | + }; |
| 53 | + int main() |
| 54 | + { |
| 55 | + Parent parent; |
| 56 | + Child child; |
| 57 | + parent.a = 1; |
| 58 | + child.a = 2; |
| 59 | + Parent* p = &child; |
| 60 | + Parent& ref_parent = parent; |
| 61 | + cout << p->a << endl; |
| 62 | + cout << ref_parent.a << endl; |
| 63 | + // 对引用指向的对象进行赋值 |
| 64 | + // 改变的是对象内部的值,a的值变了 |
| 65 | + ref_parent = child; |
| 66 | + cout << ref_parent.a << endl; |
| 67 | + p->v_test(); |
| 68 | + ref_parent.v_test(); |
| 69 | + return 0; |
| 70 | + } |
| 71 | + ``` |
| 72 | + |
| 73 | + ??? example "输出" |
| 74 | + |
| 75 | + ```plaintext |
| 76 | + 2 |
| 77 | + 1 |
| 78 | + 2 |
| 79 | + Child::v_test |
| 80 | + Parent::v_test |
| 81 | + ``` |
0 commit comments