// ============================================================================ // p1c03_35 — 3.10.5 常量引用 // 来源:川大 C++/LinuxC wiki「02.Qt方向」 // 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用 // // --- 原文片段 [pageId:blockIdx] --- 标注出处。 // 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。 // ============================================================================ #include using namespace std; // --- 原文片段 [58954439:34] 3.10.5 常量引用 --- void test01(){ int a = 100; const int& aRef = a; //此时aRef就是a //aRef = 200; 不能通过aRef的值 a = 100; //OK cout << "a:" << a << endl; cout << "aRef:" << aRef << endl; } void test02(){ //不能把一个字面量赋给引用 //int& ref = 100; //但是可以把一个字面量赋给常引用 const int& ref = 100; //int temp = 200; const int& ret = temp; } // --- main --- int main() { test01(); test02(); return 0; }