Duanxx的C++学习: const指针具体解释
Const指的是一个编译时的常量。
keywordconst使得代码能够确定一个变量能否够被改动。
使用了const后,能够防止对变量或者指针的改动;更重要的是,const的引用能够防止对所引用的对象的改动。
一般来说,在C语言中,对于一些常量的定义,我习惯性的使用define,而在C++中则最好改为使用const。
对于嵌入式程序而言,const的使用则是相当的微妙的,被const修饰后,其变量是存放在ROM中的,这一点非常重要。
关于Const的指针的使用,文字讲解没有意义,直接參见以下的代码及凝视:
#include <iostream>
// const
// - a compile time constraint that an object can not be modified int main()
{
const int i = 1;
int a = 0;
const int *p1 = &i; ///< data is const ,but pointer is not int* const p2 = &a; ///< pointer p2 itself is const ,but the data p2 point to is not const
//int* const p3 = &i; ///< illegal , cannot convert from 'const int *' to 'int *const '
int const* p4 = p1; ///< if const is on the left of *,data is const
///< if cosnt is on the right of *,pointer is const const int* const p5 = &i; ///<the data and the pointer are both const return 0;
}
Duanxx的C++学习: const指针具体解释的更多相关文章
- 两个知识点的回顾(const指针和动态链接库函数dlopen)
昨天,看了一点<c++ primer>和<程序员的自我修养>,想起了自己以前的两个知识点,这里回顾,并且总结一下. 1. const指针的参数 看primer的时候,看到几个概 ...
- 指向const的指针和const指针
1.指向const的指针:const int *p 或 int const *p 解释:p是一个指针,指向const int类型的常量:指针指向的内容为常量,因此不能改变*p的值,但指针p可以改变,指 ...
- c语言指针具体解释
指针是C语言中广泛使用的一种数据类型. 运用指针编程是C语言最基本的风格之中的一个.利用指针变量能够表示各种数据结构: 能非常方便地使用数组和字符串: 并能象汇编语言一样处理内存地址,从而编出精练而高 ...
- 算法导论学习---红黑树具体解释之插入(C语言实现)
前面我们学习二叉搜索树的时候发如今一些情况下其高度不是非常均匀,甚至有时候会退化成一条长链,所以我们引用一些"平衡"的二叉搜索树.红黑树就是一种"平衡"的二叉搜 ...
- c语言指针详细解释
指针是C语言中广泛使用的一种数据类型. 运用指针编程是C语言最基本的风格之中的一个.利用指针变量能够表示各种数据结构: 能非常方便地使用数组和字符串: 并能象汇编语言一样处理内存地址,从而编出精练而高 ...
- Java中“指针”的解释以及对“引用”的理解
Java中"指针"的解释以及对"引用"的理解 初学Java面对对象编程,对于一些概念还真的有点难以理解,主要是因为不由自主的联系到以前学过的C语言知识,时不时的 ...
- const指针
const指针这个问题,复习了好多次,每次看书的时候明白,但是过了一段时间之后没用,总会再次被搞糊涂. 那么今天就把这个问题先用代码实现,把自己理解的写下来.代码在下面: ; ; const int* ...
- 不可或缺 Windows Native (18) - C++: this 指针, 对象数组, 对象和指针, const 对象, const 指针和指向 const 对象的指针, const 对象的引用
[源码下载] 不可或缺 Windows Native (18) - C++: this 指针, 对象数组, 对象和指针, const 对象, const 指针和指向 const 对象的指针, con ...
- const 指针的三种使用方式
///////////////////////const 指针的三种状态///////////////////// 注意:const 的前后顺序 const 在类型之前 ---可以修改指针包含的地址, ...
随机推荐
- CSS 控制应为Html页面高度导致抖动的问题
在CSS中添加如下代码: html,body{ overflow-y:scroll;} html,body{ overflow:scroll; min-height:101%;} html{ over ...
- leetCode 34.Search for a Range (搜索范围) 解题思路和方法
Search for a Range Given a sorted array of integers, find the starting and ending position of a give ...
- hdu1569find the safest road(floyd变形求最大安全值)
find the safest road Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- TCP、UDP数据包大小的限制(UDP数据包一次发送多大为好)——数据帧的物理特性决定的,每层都有一个自己的数据头,层层递减
1.概述 首先要看TCP/IP协议,涉及到四层:链路层,网络层,传输层,应用层. 其中以太网(Ethernet)的数据帧在链路层 IP包在网络层 TCP或UDP包在传输层 TCP或UDP中的数据(Da ...
- JS Bin Tips and Bits • About
JS Bin Tips and Bits • About About Who built this? JS Bin was built by Remy Sharp and is completel ...
- [Android]ADT Run时候报错:The connection to adb is down, and a severe error has occured
The connection to adb is down, and a severe error has occured. 之 ..\sdk\platform-tools\adb.exe and c ...
- nginx & flup & django & python3.x @ window7配置备忘录
最近考虑原Prism建筑(非职业.半专业人士认为C/S建筑)至B/S迁移,主要是由于部署问题,包括两个因素:已经做,虽然一键安装和部署的一个因素,心存顾虑,虽然我一再声明这是一个绿色软件.还有一个因素 ...
- cocos2d-x游戏开发 跑酷(四) 关联与物理世界
原创.转载注明出处http://blog.csdn.net/dawn_moon/article/details/21451077 前面一节尽管实现了一个跑动的人物,可是他只不过一个精灵在运行一个跑动的 ...
- Invalid embedded descriptor for ".proto".Dependencies passed (Protobufer)解决办法
前言 之前开发的时候,发现居然出现了Dependencies passed to FileDescriptor.buildFrom() don't match those listed in the ...
- hdu4341(分组背包)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4341 题意:一个人在原点(0,0)抓金子,每块金子有一个获得需要的时间t和价值v.而且有的金子可能在一 ...