// ============================================================================ // p1c06_03 — 2.3 常量转换(const_cast) // 来源:川大 C++/LinuxC wiki「02.Qt方向」 // 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用 // // --- 原文片段 [pageId:blockIdx] --- 标注出处。 // 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。 // ============================================================================ #include using namespace std; // --- 原文片段 [58954473:2] 2.3 常量转换(const_cast) --- //常量指针转换成非常量指针 void test01(){ const int* p = NULL; int* np = const_cast(p); int* pp = NULL; const int* npp = const_cast(pp); const int a = 10; //不能对非指针或非引用进行转换 //int b = const_cast(a); // 不能对非指针或非引用进行 const_cast } // end of test01() //常量引用转换成非常量引用 void test02(){ int num = 10; int & refNum = num; const int& refNum2 = const_cast(refNum); } // --- main --- int main() { test01(); test02(); return 0; }