1. #include<bits/stdc++.h>
    2. using namespace std;
    3. /**
    4. * const_cast 强制类型转换
    5. * */
    6. void sqr(const int &x) { // x 是常引用
    7. // 将x由常引用暂时转为普通引用
    8. const_cast<int &>(x) = x*x; // 去掉 x 的 const 限制
    9. }
    10. int main() {
    11. int a = 5;
    12. const int b = 5;
    13. sqr(a);
    14. cout<<a<<endl;
    15. sqr(b); // 由于 b 为 const类型,对其修改无效
    16. cout<<b<<endl;
    17. return 0;
    18. }