const变量赋值原则

来源:互联网 发布:js 全局变量 丢失属性 编辑:程序博客网 时间:2024/06/05 14:10
一、关于指向const的指针
const int **p1;int *p2;p1 = &p2; //  error 非const地址无法赋值给const 指针const int temp_1 = 4;int temp_2 = 9; const int *pt_1;int *pt_2;pt_1 = &temp_1;pt_2 = &temp_1;//error const数据地址无法赋值给非const指针, 防止通过非const指针修改const数据pt_1 = &temp_2;pt_1 = &temp_1;


总结:

1.const指针可以指向const和非const数据。

2.非const指针不可以指向consts数据。

核心思想:防止const 数据被通过指针被间接修改。

原创粉丝点击