对于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的应用的更多相关文章

  1. c++ 数据类型转换: static_cast dynamic_cast reinterpret_cast const_cast

    c++ 数据类型转换: static_cast dynamic_cast reinterpret_cast const_cast  [版权声明]转载请注明出处 http://www.cnblogs.c ...

  2. C++强制类型转换操作符 const_cast

    const_cast也是一个强制类型转换操作符.<C++ Primer>中是这样描述它的: 1.将转换掉表达式的const性质. 2.只有使用const_cast才能将const性质性质转 ...

  3. static_cast、dynamic_cast、reinterpret_cast、const_cast以及C强制类型转换的区别

    static_cast 1. 基础类型之间互转.如:float转成int.int转成unsigned int等 2. 指针与void*之间互转.如:float*转成void*.CBase*转成void ...

  4. [转载]const_cast

     1. 一个经典实例 /* 用法:const_cast<type_id> (expression) 该运算符用来修改类型的const或volatile属性.除了const 或volatil ...

  5. c++强制类型转换:dynamic_cast、const_cast 、static_cast、reinterpret_cast

    c++强制类型转换:dynamic_cast.const_cast .static_cast.reinterpret_cast 博客分类: C/C++ CC++C#编程数据结构  dynamic_ca ...

  6. static_cast dynamic_cast const_cast reinterpret_cast总结对比

    [本文链接] http://www.cnblogs.com/hellogiser/p/static_cast-dynamic_cast-const_cast-reinterpret_cast.html ...

  7. static_cast, dynamic_cast, const_cast

    http://www.cnblogs.com/chio/archive/2007/07/18/822389.html 首先回顾一下C++类型转换: C++类型转换分为:隐式类型转换和显式类型转换 第1 ...

  8. 强制类型转换(const_cast)

    [1] const_cast的作用 一.常量指针被转化成非常量指针,并且仍然指向原来的对象: 二.常量引用被转换成非常量引用,并且仍然指向原来的对象: 三.常量对象被转换成非常量对象. [2] 实例代 ...

  9. C++-const_cast, reinterpret_cast, static_cast的用法

    /////////////////////////////////////////////////////////////////////////////// // // FileName : cas ...

  10. C++-const_cast只能用于指针和引用,对象的const到非const可以用static_cast

    Static_cast可以对对象也可以对指针也可以对引用,但是const_cast只可以对指针和引用使用,后者不可以对对象用,如果你要把一个const值转化为非const值只能用隐式执行或通过使用st ...

随机推荐

  1. 复习下C 链表操作(单向循环链表、查找循环节点)

    循环链表 稍复杂点. 肯能会有0 或 6 字型的单向循环链表.  接下来创建 单向循环链表 并 查找单向循环链表中的循环节点. 这里已6字型单向循环链表为例. //创建 循环链表 Student * ...

  2. iOS 在object-c 中调用c文件 方法

    1,新建c 头文件  lib.h 定义 c 函数 2,新建 c 实现文件,新建模板选中 c File  lib.c 3,oc 中调用,引用 c 头文件 lib.h ok .搞定

  3. struts2:多模块多配置文件开发

    struts2支持多模块多配置文件开发.下面是一个仅包含两个模块的示范程序,包括财务.仓库模块.它们都有一个“caiwu“的Action,在各自的命名空间下:还有一个从财务转向到仓库的Action. ...

  4. Android 设备管理API概览(Device Administration API)

    原文:http://android.eoe.cn/topic/android_sdk Android 2.2通过提供Android设备管理API的支持来引入企业应用支持.在系统级的设备管理API提供了 ...

  5. 香蕉派 banana pi BPI-M3 八核开源硬件开发板

     Banana PI BPI-M3 是一款8核高性能单板计算机,Banana PI BPI-M3是一款比树莓派 2 B更强悍的8核Android 5.1产品. Banana PI BPI-M3 兼 ...

  6. vim的Tab键

    vim中默认的tab键大约是6个空格(目测)的宽度.如果想修改为4个空格,用以下命令:    shiftwidth=4    softtabstop-4shiftwidth的含义是:回车后需要缩进时, ...

  7. mybatis 对象关系映射例子

    入门 http://legend2011.blog.51cto.com/3018495/908956 增删改 http://legend2011.blog.51cto.com/3018495/9130 ...

  8. Debugging the Java HotSpot VM

    Open Heart Surgery: Analyzing and Debugging the Java HotSpot VM at the OS Level https://www.youtube. ...

  9. Replace 在动态sql中的实现

    set @stsqlReplace=' update ChgCfm set cfmdate=replace(cfmdate,'''''''',''''), cfmstatu=replace(cfmst ...

  10. Testng生成的测试报告乱码解决办法

    Testng生成的测试报告乱码解决办法 2017-06-16 1 问题描述 乱码是程序编码不统一,比如Java源代码是utf-8,编译是gbk,这时会乱码. 代码如下: org.testng.Repo ...