// ============================================================================ // p1c03_27 — 3.9.3 尽量以const替换#define // 来源:川大 C++/LinuxC wiki「02.Qt方向」 // 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用 // // --- 原文片段 [pageId:blockIdx] --- 标注出处。 // 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。 // ============================================================================ #include using namespace std; // --- 原文片段 [58954439:26] 3.9.3 尽量以const替换#define --- void func1(){ const int a = 10; #define A 20 //#undef A //卸载宏常量A } void func2(){ //cout << "a:" << a << endl; //不可访问,超出了const int a作用域 cout << "A:" << A << endl; //#define作用域从定义到文件结束或者到#undef,可访问 } int main(){ func2(); return EXIT_SUCCESS; } // --- main ---