C++ const pointer
在C++中const限定的指针类型常常令人困惑,现整理如下,以整型为例,主要区分如下三个例子
const int * p; int * const p; const int * const p;
其实就是2种情况,const在int前及const在变量名前,第三种及叠加了前两种限定
CONST概念
const即constant缩写,用于表示所限定的变量不能改变
CONST与指针
在前面的例子中,如果将p指向一个变量val,则无论是哪一个例子,变量val本身并不受指向它的指针的类型限定影响,可以任意改变
const int *
int b = 2; int bb = 22; const int * const_p = &b; // (*const_p)++; // error const_p = &bb;
在这种限定下,表示所限定的指针对指向的变量只有只读权限(变量自身可以改变),但可以改变指针自身,即指向另一个变量
int * const
int c = 3; int cc = 33; int * const p_const = &c; (*p_const)++; // p_const = &cc; // error
在这种限定下,表示所限定的指针自身不能改变,即不能指向别的变量,但是可以通过指针去修改指向的变量
const int * const
叠加了上面两个限制
演示代码
1 #include <iostream> 2 3 using namespace std; 4 5 int main(){ 6 // non const 7 int a = 1; 8 int aa = 11; 9 int * p = &a; 10 11 cout << "a = " << a << "; p point to a, *p = " << *p << endl; 12 13 a++; 14 cout << "\tafter a++" << endl; 15 cout << "a = " << a << "; p point to a, *p =" << *p << endl; 16 17 (*p)++; 18 cout << "\tafter (*p)++" << endl; 19 cout << "a = " << a << "; p point to a, *p= " << *p << endl; 20 21 p = &aa; 22 cout << "\tafter p = &aa" << endl; 23 cout << "a = " << a << ", aa = " << aa << "; p point to aa, *p = " << *p << endl; 24 25 26 // const int * 27 int b = 2; 28 int bb = 22; 29 const int * const_p = &b; 30 31 cout << endl << "now switch to 'const int *'" << endl; 32 cout << "b = " << b << "; const_p point to b, *const_p = " << *const_p << endl; 33 34 b++; 35 cout << "\tafter b++" << endl; 36 cout << "b = " << b << "; const_p point to b, *const_p = " << *const_p << endl; 37 38 // (*const_p)++; // error: increment of read-only location '* const_p' 39 cout << "\t(*const_p)++: error: increment of read-only location '* const_p'" << endl; 40 41 const_p = &bb; 42 cout << "\tafter const_p = &bb" << endl; 43 cout << "b = " << b << ", bb = " << bb << "; const_p point to bb, *const_p = " << *const_p << endl; 44 45 46 // int * const 47 int c = 3; 48 int cc = 33; 49 int * const p_const = &c; 50 51 cout << endl << "now switch to 'int * const'" << endl; 52 cout << "c = " << c << "; p_const point to c, *p_const = " << *p_const << endl; 53 54 c++; 55 cout << "\tafter c++" << endl; 56 cout << "c = " << c << "; p_const point to c, *p_const = " << *p_const << endl; 57 58 (*p_const)++; 59 cout << "\tafter (*p_const)++" << endl; 60 cout << "c = " << c << "; p_cosnt point to c, *p_const = " << *p_const << endl; 61 62 // p_const = &cc; // error: assignment of read-only variable 'p_const' 63 cout << "\tp_const = &cc: error: assignment of read-only variable 'p_const'" << endl; 64 65 66 // const int * cosnt 67 int d = 4; 68 int dd = 44; 69 const int * const const_p_const = &d; 70 71 cout << endl << "now switch to 'const int * const'" << endl; 72 cout << "d = " << d << "; const_d_const point to d, *const_p_const = " << *const_p_const << endl; 73 74 d++; 75 cout << "\tafter d++" << endl; 76 cout << "d = " << d << "; const_d_const point to d, *const_p_const = " << *const_p_const << endl; 77 78 // (*const_p_const)++; // error: increment of read-only location '*(const int*)const_p_const' 79 cout << "\t(*const_p_const)++: error: increment of read-only location '*(const int*)const_p_const'" << endl; 80 81 // const_p_const = &cc; // error: assignment of read-only variable 'const_p_const' 82 cout << "\tconst_p_const = &dd: error: assignment of read-only variable 'const_p_const'" << endl; 83 84 return 0; 85 }