在C++中指针与const的组合是C++笔试和面试中常见问题,现将其总结如下:一共有六种形式
(1)const int p;
(2)const int* p;
(3)int const* p;
(4)int * const p;
(5)const int * const p;
(6)int const * const p;
对于第一种,没有什么哈奥说的,表示整形常量
对于后面5种,有种简单的记忆方法:从后面往前面读,遇到p就读做“ p is a” ,“*”就读作 point to
(1)第二种就可以读做: p is a point to int const 翻译过来就是:p是一个指向整形常量的指针
(2)第三种就可以读做: p is a point to const int 意思与2相同
(3)第四种就可以读做:p is a const point to int p是一个常量指针,指向整型。
(4) 第五种就可以读做: p is a const point to int const p是一个常量指针,指向整形常量
(5)第六种就可以读做: p is a const point to int const p是一个常量指针,指向常量整数,与第五种意思一样
浙公网安备 33010602011771号