四句话解决const赋值问题
2016-07-09 12:32
363 查看
有修饰自己的const就不能再被赋值,且一定要初始化(reference同样)
等号右边修饰自己的const在赋值时会被忽略
指针的类型有const type(不能用指针修改所指内容)和 type(可以)
其中 type类型的指针可以赋值给const type类型,因为这样做相容,反过来就不行。想想也是,从可以修改指向的内容到不能修改所指范围变窄了。
注:
修饰自己的const 指的是 限定符const
type 指的是 数据类型,如int等
要注意指针的const int是类型!!千万不要把const int类型跟限定符const搞混,如果你觉得各种const很混乱就是这里没理解好!
在《C++ primier》中采用了顶层const和底层const这两种说法,我觉得这样更容易弄混,他把const int中的const断章取义单独把const拎出来起了个名字叫底层const。至于顶层const就是指限定符const。
这四句话从《c专家编程》中总结出来
书中说到赋值的条件:
两个操作数都是指向有限定符或无限定符的相容类型的指针,左边指针所指向的类型必须具有右边指针所指向类型的全部限定符。
自行检验我说的对不对,变量命名还是用了top_level_const 和low_level const这种说法(不然真不知道怎么命名)
带有lc字样的是他是const type类型,因此不能修改所指的内容
带有tc字样的是带有限定符const,因此本身不能被修改
在函数参数传递的时候喜欢加const来确保安全性
int a;
void fun1(const int a){}
fun1(a); //从没有限定符const变为有限定符const,可以,这很安全
void fun2(const int& r){}
fun2(a);//const int类型确保了r不能够修改a,可以,这很安全
void fun3(const int* lc_p){}
fun3(&a);//const int*类型确保了lc_p不能修改a,可以,这很安全
void fun4(const int* const lc_tp_p){}
fun4(&a);//即不能通过lc_tp_p修改a,又不能用一个新的指针赋值给lc_tp_p,可以,这非常安全
等号右边修饰自己的const在赋值时会被忽略
指针的类型有const type(不能用指针修改所指内容)和 type(可以)
其中 type类型的指针可以赋值给const type类型,因为这样做相容,反过来就不行。想想也是,从可以修改指向的内容到不能修改所指范围变窄了。
注:
修饰自己的const 指的是 限定符const
type 指的是 数据类型,如int等
要注意指针的const int是类型!!千万不要把const int类型跟限定符const搞混,如果你觉得各种const很混乱就是这里没理解好!
在《C++ primier》中采用了顶层const和底层const这两种说法,我觉得这样更容易弄混,他把const int中的const断章取义单独把const拎出来起了个名字叫底层const。至于顶层const就是指限定符const。
这四句话从《c专家编程》中总结出来
书中说到赋值的条件:
两个操作数都是指向有限定符或无限定符的相容类型的指针,左边指针所指向的类型必须具有右边指针所指向类型的全部限定符。
自行检验我说的对不对,变量命名还是用了top_level_const 和low_level const这种说法(不然真不知道怎么命名)
带有lc字样的是他是const type类型,因此不能修改所指的内容
带有tc字样的是带有限定符const,因此本身不能被修改
40 void c_r(void){ 41 int i; 42 const int tc_i = 0; //不能修改tc_i 43 int& r = i; //reference引用 44 const int& lc_r = i; //const int类型的引用 45 int* p; 46 const int* lc_p; //const int* 类型 47 int* const tc_p = &i ;//int* 类型 48 const int* const lc_tc_p = &i ;//int* 类型,带有限定符const 49 50 i = tc_i;//在赋值时限定符const会被忽略,所以这样做比较危险,特别是在初始化函数形参的时候会失去限定符const 51 //p = lc_p; //error invalid conversion from 'const int*' to 'int*'不相容 52 lc_p = p; //相容 53 54 //lc_r = r; //error all the assignment of reference is invalid 不能修改带有限定符const的(或reference) 55 const int& lc_r2 = r;//相容 56 //int& r2 = lc_r; //invalid initialization of reference of type ‘int&’ from expression of type ‘const int’ 类型不相容 57 //so 因此 58 const int& lr_i = 44; //ok,44是const int 59 //int& r_i = 44;//invalid initialization of non-const reference of type ‘int&’ from an rvalue of type ‘int’ 60 }
在函数参数传递的时候喜欢加const来确保安全性
int a;
void fun1(const int a){}
fun1(a); //从没有限定符const变为有限定符const,可以,这很安全
void fun2(const int& r){}
fun2(a);//const int类型确保了r不能够修改a,可以,这很安全
void fun3(const int* lc_p){}
fun3(&a);//const int*类型确保了lc_p不能修改a,可以,这很安全
void fun4(const int* const lc_tp_p){}
fun4(&a);//即不能通过lc_tp_p修改a,又不能用一个新的指针赋值给lc_tp_p,可以,这非常安全
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- 关于指针的一些事情
- c++ primer 第五版 笔记前言
- share_ptr的几个注意点
- Linux C函数参考手册(PDF版)
- Jquery 表单取值赋值的一些基本操作
- C# partial关键字说明
- Lua中调用C++函数示例
- Lua教程(十七):C API简介
- 简单谈谈lua和c的交互
- Lua教程(一):在C++中嵌入Lua脚本
- C#用链式方法表达循环嵌套
- Ruby编程中的赋值相关操作
- Lua教程(二):C++和Lua相互传递数据示例
- PowerShell中定义多个变量并赋值的例子
- C#中的委托数据类型简介
- C++联合体转换成C#结构的实现方法
- C#编写的艺术字类实例代码
- C#实现打造气泡屏幕保护效果