这是本文档旧的修订版!
const是单词constant的简写,字面意思是常数、常量。用于变量修饰,表明这个变量不能被修改;用于指针修饰,表明指针的指向物不能被修改;用于方法修饰,表明这个方法不会对对象造成改变。
const int foo = 1; foo = 2; // compile time error const int* ptr = &foo; *ptr = 3 // compile time error int fuck = 0; ptr = &fuck; // this is OK *ptr = 123; // compile time error struct FooBar { int member; int MyMethod(int value) const { member = value; // compile time error } };