顶层const和底层const
来源:互联网 发布:三菱步进梯形图编程 编辑:程序博客网 时间:2024/05/10 10:08
顶层const表示指针本身是个变量,底层const表示指针所指的对象是一个常量。
int i = 0;int *const p1 = &i; //顶层constconst int ci = 42; //顶层constconst int *p2 = &ci; //底层const,允许改变p2的值,不允许改变ci的值const int *const p3 = p2; //靠右的const是顶层const,修饰p3,靠左的是底层const,修饰p2.const int &r = ci; //用于声明引用的const都是底层const注意拷贝时顶层const和底层const的区别:
i = ci; //正确:拷贝ci的值,ci是一个顶层const,对此操作无影响p2 = p3; //正确:p2和p3指向的对象类型相同,p3顶层const的部分不影响int *p = p3; //错误:p3包含底层const的定义,而p没有p2 = p3; //正确:都是底层constp2 = &i; //正确:int*能转换为const int*int &r = ci; //错误:普通的int&不能绑定到int常量上const int &r2 = i; //正确:const int&可以绑定到一个普通的int上
0 0
- 底层const和顶层const
- 顶层const和底层const
- 顶层const和底层const
- 顶层const和底层const
- 顶层const和底层const
- 顶层const和底层const
- 顶层const和底层const
- 顶层const和底层const
- 顶层const和底层const
- 顶层const和底层const
- 顶层const 底层const
- c++顶层和底层const
- C++中顶层const和底层const
- 顶层const和底层const的理解
- C++中的顶层const和底层const
- C++中顶层const和底层const
- “顶层const”和“底层const”的思考
- C++中顶层const和底层const
- 使用Matplotlib绘图
- hdu5606tree
- matlab---数据拟合
- 制作initrd(5):解剖Ubuntu安装盘
- Python笔记(1)
- 顶层const和底层const
- UVA 674 coin change
- 给 Android 开发者的 RxJava 详解
- HNOI 2013 bzoj 3143 游走
- PTA--哈利·波特的考试--最短路径--Flord算法
- [QT]拖拽改变控件大小
- 多线程
- Educational Codeforces Round 8 E. Zbazi in Zeydabad(BIT)
- 最近看的一本书和两部电影