const的定义:常类型是指使用类型修饰符const说明的类型,常类型的变量或对象的值是不能被更新的。
为什么函数中要引用const?
回:const 推出的初始目的,正是为了取代预编译指令,消除它的缺点,同时继承它的优点。
1、const把变量变为只读,对于基本数据类型对于const是透明的。
int a;
const int ca=10;
a=ca;//合法
ca=20;//error
ca=1;//error
2、const 修饰指针
(1) const int *p1=&a;
p1=&a;
*p1=100;//error
(2)int const *p=&a;
(1)等同于(2)
3、权限可以同等或缩小传递但不能放大传递。
int a=10;
const int ca=10;
int *p1=&a
int*p2=&ca//error
int*const p5=&a
int *const p6=&ca//error