int a = 10;
1) const int* b = &a;
2) int const *c = &a;
3) int* const d = &a;
4) const int* const e = &a;
// 1),2) 指针所指向的内容为常量这种情况下不允许对内容进行更改操作
*b = 20;
*c = 30;
// 3) 指针是常量这种情况下不能对指针本身进行更改操作,如d++是错误的
d++;
// 4) 指针本身和指向的内容均为常量
一般来说,你可以在头脑里画一条垂直线穿过指针声明中的星号(*)位置,如果const出现在线的左边,指针指向的数据为常量;如果const出现在线的右边,指针本身为常量;如果const在线的两边都出现,二者都是常量。参考《Effective c++》Item21。