const关键字的作用

原链接:https://blog.csdn.net/lf1570180470/article/details/56677748

const:只读  不可修改

const在谁后面,谁就不能被修改,如果const在最前面,则将其后移一位。

应用:

1.阻止一个变量被改变,const修饰,一旦初始化,无法更改。

2.指针,指针本身为const,指针所指的数据为const,或两者同时为const。

3.函数声明中,形参为const,在函数内部不可被修改。

4.类成员函数,const类型为常函数,不能改变类的成员变量。

5.类成员函数,返回const类型,表示其返回值不能为“左值”。

猜你喜欢

转载自blog.csdn.net/qq_17141957/article/details/80174707