// ============================================================================ // p1c03_33 — 3.10.3 引用的本质 // 来源:川大 C++/LinuxC wiki「02.Qt方向」 // 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用 // // --- 原文片段 [pageId:blockIdx] --- 标注出处。 // 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。 // ============================================================================ #include using namespace std; // --- 原文片段 [58954439:32] 3.10.3 引用的本质 --- //发现是引用,转换为 int* const ref = &a; void testFunc(int& ref){ ref = 100; // ref是引用,转换为*ref = 100 } int main(){ int a = 10; int& aRef = a; //自动转换为 int* const aRef = &a;这也能说明引用为什么必须初始化 aRef = 20; //内部发现aRef是引用,自动帮我们转换为: *aRef = 20; cout << "a:" << a << endl; cout << "aRef:" << aRef << endl; testFunc(a); return EXIT_SUCCESS; } // --- main ---