C++转换运算符之const_cast
来源:互联网 发布:forest类似的软件 编辑:程序博客网 时间:2024/06/07 03:16
C++提供了四个转换运算符:
- const_cast <new_type> (expression)
- static_cast <new_type> (expression)
- reinterpret_cast <new_type> (expression)
- dynamic_cast <new_type> (expression)
c 语言中怎么去除 const 修饰? 比如:const int value=0.2f; int *ptr;ptr 怎么样获取 value 的值?
答:const* int const_p=&value;
int* ptr=const_cast(const_p);
两种错误的方式:(1) int modify=&value;(2) int* modify=&value;
传统方式可写成: int* modify=(int*) &value;
这部分详细解释可参考http://www.cnblogs.com/ider/archive/2011/07/22/cpp_cast_operator_part2.html
这样修改完成后value还是保留了它原来的值,可是它们指向了同一个地址.此时我们不能修改value的值可是能修改modify的值
为何要去除const限定
(1)我们可能调用了一个参数不是const的函数,而我们要传进去的实际参数确实const的,但是我们知道这个函数是不会对参数做修改的。于是我们就需要使用const_cast去除const限定,以便函数能够接受这个实际参数。(2)定义了一个非const的变量,但用带const限定的指针去指向它,在某一处我们突然又想修改了,可是我们手上只有指针,这时候我们可以去const来修改了。
0 0
- C++转换运算符之const_cast
- C++类型转换运算符之const_cast<newtype>(expression)
- C++标准转换运算符const_cast
- C++标准转换运算符const_cast
- C++标准转换运算符const_cast
- C++标准转换运算符const_cast
- C++标准转换运算符const_cast
- C++标准转换运算符const_cast
- C++标准转换运算符const_cast
- C++标准转换运算符const_cast
- C++标准转换运算符const_cast
- C++标准转换运算符const_cast
- C++标准转换运算符const_cast
- C++标准转换运算符const_cast
- C++标准转换运算符const_cast
- C++标准转换运算符const_cast
- C++标准转换运算符const_cast
- C++标准转换运算符const_cast
- 基于memcache的分布式缓存操作
- AssignmentOperator
- Jetty设置请求表单最大值
- 无题
- Android中常常使用shape来定义控件的一些显示属性
- C++转换运算符之const_cast
- ABBYY FineReader 12 能够识别这些文档语言吗
- 202.leetcode Happy Number(easy)[数学 快乐数]
- C#之XML基础 读取一个现有的xml文件,并输出其中的内容
- 如何在阿里云上部署django网站(3)——runserver试运行
- 对miniUI的后台读取数据理解
- android.os.TransactionTooLargeException
- Oracle删除表锁
- assgin ,copy,retain