对于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. Atitit 转移特效attilax总结

    Atitit 转移特效attilax总结 总结一般从按钮,转移到大点的方框上回比较好看点.. <!--jq ui--> <style type="text/css" ...

  2. Atitit jquery  1.4--v1.11  v1.12  v2.0  3.0 的新特性

    Atitit jquery  1.4--v1.11  v1.12  v2.0  3.0 的新特性 1.1. Jquery1.12  jQuery 2.2 和 1.12 新版本发布 - OPEN资讯.h ...

  3. FFmpeg(9)-解码器解码代码演示(FFmpeg调用MediaCodec实现硬解码、多线程解码、及音视频解码性能测试)

    一.AVFrame 用来存放解码后的数据. [相关函数] AVFrame *frame = av_frame_alloc();                       // 空间分配,分配一个空间 ...

  4. Oracle根据表生成系统流水号

    1.建表tablewater create table TABLEWATER ( tb_id INTEGER not null, vc_table_name ), num_water_no ) )vc ...

  5. Java中使用Oracle的客户端 load data和sqlldr命令执行数据导入到数据库中

    Windows环境下测试代码: import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundExcep ...

  6. linux命令(40):基础常用命令:cd,rm,mk

    常用命令介绍       pwd,显示当前在哪个路径下 linux的用户管理 :       useradd 用户名,添加用户       [案例]useradd xiaoming       pas ...

  7. 【Acm】算法之美—Anagrams by Stack

    题目概述:Anagrams by Stack How can anagrams result from sequences of stack operations? There are two seq ...

  8. 组策略分发Adobe Reader 11教程

    1, 实验环境 域控:Windows Server 2012 R2 客户端:Windows 7 Adobe Reader 版本:11.0.00 2, 获取分发 Adobe Reader 的许可协议 按 ...

  9. JS实现多行文本最后是省略号紧随其后还有个超链接在同一行的需求

    1.布局及样式如下图: 2.js获得上图的div对象,然后判断div对象的高度,如果大于一行的高度了表示内容有两行了,再获得span标签里面的内容并用正则将后六个字符替换成“......”这里的实现代 ...

  10. php 裁剪图片类

    <?php /* *说明:函数功能是把一个图像裁剪为任意大小的图像,图像不变形 * 参数说明:输入 需要处理图片的 文件名,生成新图片的保存文件名,生成新图片的宽,生成新图片的高 * writt ...