// 04_const_demo.cpp
// 演示 const 变量、指针、引用和 constexpr 的各种用法

#include <iostream>

constexpr int square(int x) {
    return x * x;
}

int main() {
    // --- const 变量 ---
    const int kMaxSize = 100;
    std::cout << "kMaxSize = " << kMaxSize << std::endl;

    // --- constexpr ---
    constexpr int kArraySize = square(5); // 编译期计算，结果为 25
    std::cout << "kArraySize = " << kArraySize << std::endl;

    // --- 指向常量的指针 ---
    int a = 10;
    int b = 20;
    const int* p_to_const = &a;
    p_to_const = &b; // 没问题，指针可以改指向
    std::cout << "*p_to_const = " << *p_to_const << std::endl;

    // --- 常量指针 ---
    int* const const_p = &a;
    *const_p = 100; // 没问题，可以改数据
    std::cout << "*const_p = " << *const_p << std::endl;

    // --- 两个都 const ---
    const int* const double_const = &a;
    std::cout << "*double_const = " << *double_const << std::endl;

    // --- const 引用 ---
    int x = 42;
    const int& ref = x;
    x = 100;                                   // 直接改 x 是可以的
    std::cout << "ref = " << ref << std::endl; // 输出 100

    return 0;
}
