博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
类型转换(2)
阅读量:4360 次
发布时间:2019-06-07

本文共 1033 字,大约阅读时间需要 3 分钟。

const_cast转换符是用来移除变量的const或volatile限定符

const_cast就可以直接使用显示转换(int*)来代替:

#include<iostream>

using namespace std;

int main()

{
    const int a = 1; //a的值永远不变
    const int* t = &a; //t的值永远不变,但是*t可能改变
    int* b = const_cast<int*>(t);//效果和int* b = (int*)(t)一样
    *b = 2;
   
    cout<<a<<"\t"<<*t<<"\t"<<*b<<endl;
    cout<<&a<<"\t"<<t<<"\t"<<b<<endl;
}

--------------------------------------------------------------------------------

1    2    2

0xbfc96b1c    0xbfc96b1c    0xbfc96b1c

--------------------------------------------------------------------------------

 

#include<iostream>

using namespace std;

int main()

{
    const int a = 1;
    int c = 2;
    const int* t = &a;
    int* b = const_cast<int*>(t);
    //*b = 2;
    b = &c;
    cout<<a<<"\t"<<*t<<"\t"<<*b<<endl;
    cout<<&a<<"\t"<<t<<"\t"<<b<<endl;
}  

--------------------------------------------------------------------------------

1    1    2

0xbfb46b5c    0xbfb46b5c    0xbfb46b58

--------------------------------------------------------------------------------

从这里我们可以很明显的看到其中的区别

转载于:https://www.cnblogs.com/dashingman/archive/2012/02/14/2351337.html

你可能感兴趣的文章
Web应用漏洞评估工具Paros
查看>>
Git 和 Github 使用指南
查看>>
20180925-4 单元测试
查看>>
mysql的数据存储
查看>>
[转载] Activiti Tenant Id 字段释疑
查看>>
[Java 8] (8) Lambda表达式对递归的优化(上) - 使用尾递归 .
查看>>
SQL Server-聚焦移除Bookmark Lookup、RID Lookup、Key Lookup提高SQL查询性能
查看>>
最小权限的挑战
查看>>
jquery 视觉特效(水平滚动图片)
查看>>
SVG笔记
查看>>
linux下使用dd命令写入镜像文件到u盘
查看>>
物联网架构成长之路(8)-EMQ-Hook了解、连接Kafka发送消息
查看>>
2018-2019-1 20165234 20165236 实验二 固件程序设计
查看>>
IDEA的GUI连接数据库写入SQL语句的问题总结
查看>>
Xpath在选择器中正确,在代码中返回的是空列表问题
查看>>
leecode第一百九十八题(打家劫舍)
查看>>
【BZOJ 1233】 [Usaco2009Open]干草堆tower (单调队列优化DP)
查看>>
07-3. 数素数 (20)
查看>>
写一个欢迎页node统计接口Py脚本(邮件,附件)-py
查看>>
计算两个日期之间的天数
查看>>