C++中const关键字修饰指针
const默认作用于其左边的东西,否则作用于其右边的东西
- (a)
const int *
const只有右边有东西,所以const修饰int成为常量整型,然后* 再作用于常量整型。所以这是a pointer to a constant integer(指向一个整型,不可通过该指针改变其指向的内容,但可改变指针本身所指向的地址)。从右向左理解,把*理解为pointer to
- (b)
int const *
const 左边有int,所以const修饰int成为常量整型,然后* 再作用于常量整形(同a)。所以这也是一个a pointer to a constant integer(指向一个整型,不可通过该指针改变其指向的内容,但可改变指针本身所指向的地址)。从右向左理解,把*理解为pointer to
- (c)
int * const
const 左边有*,所以const先修饰指针,表示该指针指向的位置不可更改。a constant pointer to an integer
- (d)
int const * const
左边的const修饰int ,右边的const修饰 *,a constant pointer to an constant integer,表示该指针指向位置存储的内容和指向位置均不可改变
- (e)
const int * const
左边的const左边没内容,所以先修饰int ,右边的const修饰 *, 同(d), 表示 a constant pointer to a constant interger,该指针指向位置存储的内容和指向位置均不可改变
浙公网安备 33010602011771号