顶层const & 底层const

顶层const & 底层const

个人理解:顶层cosnt 就是不影响变量类型,作为修饰符的存在。底层则会影响到变量类型

例如:

int *pi; //这是一个int *类型的变量,一个指向int型的指针。
int *const p1; //这还是一个int *类型的变量(首先p1是一个指针类型,指向的是int类型的值。才不管你这个变量是不是有const修饰符)
int const *p2; //这是一个int const * 类型的变量(p2是一个指针类型,指向的是int const 类型的值)

p1 的就是一个顶层const ,p2的则是底层const。

在g++ 下得到了验证

 

 

 像 int const i; const doulbe j;等等不涉及到指针的都是顶层const

c++ 里面的重载函数如果像以下 这样声明两个同名函数是会报错的:

int fun(type name);
int fun(type const name); 

int fun2(type * name);
int fun2(type *const name);
 因为实际上是一种类型的形参,而重载函数要两个函数的形参相异。

posted @ 2019-09-05 11:31  BMing  阅读(245)  评论(0编辑  收藏  举报