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 }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章