C++ 常量局部变量
示例
声明和用法。
//a是constint,因此无法更改 const int a = 15; a = 12; //错误:无法将新值分配给const变量 a += 1; //错误:无法将新值分配给const变量
引用和指针的绑定
int &b = a; //错误:无法将非常量引用绑定到常量变量
const int &c = a; //好;c是const引用
int *d = &a; //错误:无法将指向非const的指针绑定到const变量
const int *e = &a //好;e是指向常量的指针
int f = 0;
e = &f; //好;e是非常量指向常量的指针,
//这意味着它可以反弹到新的int*或constint*
*e = 1 //错误:e是指向const的指针,这意味着
//它指向的值不能通过取消引用e来更改
int *g = &f;
*g = 1; //好;该值仍然可以通过取消引用来更改
//指向常量的指针