const_cast的应用
对于const变量,我们不能修改它的值,这是这个限定符最直接的表现。但是我们就是想违背它的限定希望修改其内容怎么办呢?于是我们可以使用const_cast转换符是用来移除变量的const限定符。
const_cast类型转换能够剥离一个对象的const属性,也就是说允许你对常量进行修改。
#include<iostream>
using namespace std; /*
用法:const_cast<type_id> (expression)
该运算符用来修改类型的const或volatile属性。除了const 或volatile修饰之外, type_id和expression的类型是一样的。
一、常量指针被转化成非常量指针,并且仍然指向原来的对象;
二、常量引用被转换成非常量引用,并且仍然指向原来的对象;
三、常量对象被转换成非常量对象。
type_id 必须为指针或引用
*/ class B
{
public:
int m_iNum;
B() : m_iNum()
{ }
}; void foo()
{
const B *b1 = new B();
//b1->m_iNum = 100; // 编译错误
// 做如下转换,体现出转换为指针类型
B *b2 = const_cast<B*>(b1);
b2->m_iNum = ;
cout<<"b1: "<< b1->m_iNum <<endl;
cout<<"b2: "<< b2->m_iNum <<endl; const B b3;
//b3.m_iNum = 100; // 编译错误
B b4 = const_cast<B&>(b3); // b4是另外一个对象
b4.m_iNum = ;
cout<<"b3: "<<b3.m_iNum <<endl;
cout<<"b4: "<<b4.m_iNum <<endl; const B b5;
//b5.m_iNum = 100; // 编译错误 // 或者左侧也可以用引用类型,如果对b6的数据成员做改变,就是对b5的值在做改变
B &b6 = const_cast<B&>(b5);
b6.m_iNum = ;
cout<<"b5: "<<b5.m_iNum <<endl;
cout<<"b6: "<<b6.m_iNum <<endl; // force to convert
const int x = ;
int* y = (int *)(&x); // 同样的地址,但是内容是不一样的
*y = ;
cout << "x: "<<x<<" address: "<<&x<<endl;
cout << "*y: "<<*y<<" address: "<<y<<endl;
cout<<endl; const int xx = ;
int* yy = const_cast<int *> (&xx); // 同样的地址,但是内容是不一样的
*yy = ;
cout << "xx: "<<xx<<" address: "<<&xx<<endl;
cout << "*yy: "<<*yy<<" address: "<<yy<<endl;
cout<<endl;
// int
const int xxx = ;
int yyy = const_cast<int&> (xxx); // yyy是另外一个int对象
yyy = ;
cout << "xxx: "<<xxx<<" address: "<<&xxx<<endl;
cout << "yyy: "<<yyy<<" address: "<<&yyy<<endl;
} int main(void)
{
foo();
return ;
}
运行结果如下:
const_cast的应用的更多相关文章
- c++ 数据类型转换: static_cast dynamic_cast reinterpret_cast const_cast
c++ 数据类型转换: static_cast dynamic_cast reinterpret_cast const_cast [版权声明]转载请注明出处 http://www.cnblogs.c ...
- C++强制类型转换操作符 const_cast
const_cast也是一个强制类型转换操作符.<C++ Primer>中是这样描述它的: 1.将转换掉表达式的const性质. 2.只有使用const_cast才能将const性质性质转 ...
- static_cast、dynamic_cast、reinterpret_cast、const_cast以及C强制类型转换的区别
static_cast 1. 基础类型之间互转.如:float转成int.int转成unsigned int等 2. 指针与void*之间互转.如:float*转成void*.CBase*转成void ...
- [转载]const_cast
1. 一个经典实例 /* 用法:const_cast<type_id> (expression) 该运算符用来修改类型的const或volatile属性.除了const 或volatil ...
- c++强制类型转换:dynamic_cast、const_cast 、static_cast、reinterpret_cast
c++强制类型转换:dynamic_cast.const_cast .static_cast.reinterpret_cast 博客分类: C/C++ CC++C#编程数据结构 dynamic_ca ...
- static_cast dynamic_cast const_cast reinterpret_cast总结对比
[本文链接] http://www.cnblogs.com/hellogiser/p/static_cast-dynamic_cast-const_cast-reinterpret_cast.html ...
- static_cast, dynamic_cast, const_cast
http://www.cnblogs.com/chio/archive/2007/07/18/822389.html 首先回顾一下C++类型转换: C++类型转换分为:隐式类型转换和显式类型转换 第1 ...
- 强制类型转换(const_cast)
[1] const_cast的作用 一.常量指针被转化成非常量指针,并且仍然指向原来的对象: 二.常量引用被转换成非常量引用,并且仍然指向原来的对象: 三.常量对象被转换成非常量对象. [2] 实例代 ...
- C++-const_cast, reinterpret_cast, static_cast的用法
/////////////////////////////////////////////////////////////////////////////// // // FileName : cas ...
- C++-const_cast只能用于指针和引用,对象的const到非const可以用static_cast
Static_cast可以对对象也可以对指针也可以对引用,但是const_cast只可以对指针和引用使用,后者不可以对对象用,如果你要把一个const值转化为非const值只能用隐式执行或通过使用st ...
随机推荐
- python 元类(metaclass)
元类参见老师的博客 http://www.cnblogs.com/linhaifeng/articles/8029564.html
- 【Unity】12.1 基本概念
开发环境:Win10.Unity5.3.4.C#.VS2015 创建日期:2016-05-09 一.简介 导航网格(Navmesh)是世界坐标系中几何体的简化表示,被游戏代理用来进行全局导航.通常,代 ...
- 精通Docker的50个必备教程、工具、资源
Docker 已经震惊了软件开发界.它提供了一种根据 DevOps 方法打包和输送应用程序的便捷方法. 最近我们发布了 51 个必备的 Docker 工具列表①,但工具不是完全精通容器化所需的唯一东西 ...
- Windows: 打开关闭网络连接的方法
在Cmd中键入 netsh interface set interface name="本地连接" admin=disablednetsh interface set interf ...
- c#,asp.net 开发 app 学习资料整理
VS2015 Apache Cordova第一个Android和IOS应用 http://www.cnblogs.com/aehyok/p/4116410.html PhoneGap:免费开源的 HT ...
- Beginning SDL 2.0(6) 音频渲染及wav播放
前面几篇关于SDL的文章介绍的是以画面为主,这里介绍下SDL中针对音频播放提供的机制,以及如何应用. 对于音频而言,有几个概念需要事先了解下,采样率.声道数.量化位数,如果你不清楚的话,麻烦先了解下这 ...
- js中取小数整数部分函数;取小数部分
1.丢弃小数部分,保留整数部分 parseInt(23.56); 结果:23 2.向上取整,有小数就整数部分加1 Math.ceil(23.56) 结果:24 3,四舍五入. Math.round(2 ...
- maven invalid loc header
项目部署之后出现如题错误 清除maven资源库原有的jar,重新下载即可解决
- Phrase-Based & Neural Unsupervised Machine Translation基于短语非监督机器翻译
1. 前言 本文介绍一种无监督的机器翻译的模型.无监督机器翻译最早是<UNSUPERVISED NEURAL MACHINE TRANSLATION>提出.这个模型主要的特点,无需使用平行 ...
- 6. 支持向量机(SVM)核函数
1. 感知机原理(Perceptron) 2. 感知机(Perceptron)基本形式和对偶形式实现 3. 支持向量机(SVM)拉格朗日对偶性(KKT) 4. 支持向量机(SVM)原理 5. 支持向量 ...