1. 定义常量
const int MAX_VAL = 23; const string NAME = "Micael Jordan";
2. 定义常量指针
int n,m; const int * p = & n; * p = 5; //编译出错 n = 4; //ok p = &m; //ok, 常量指针的指向可以变化
const int * p1; int * p2; p1 = p2; //ok p2 = p1; //error p2 = (int * ) p1; //ok,强制类型转换
void MyPrintf( const char * p ) { strcpy( p,"this"); //编译出错 printf("%s",p); //ok }
3. 定义常引用
int n; const int & r = n; r = 5; //error n = 4; //ok
第一周 从C走进C++ 005 const,布布扣,bubuko.com
原文:http://www.cnblogs.com/qingsiburan/p/3845042.html