对于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. PHP取整,四舍五入取整、向上取整、向下取整、小数截取

    PHP取整数函数常用的四种方法: 1.直接取整,舍弃小数,保留整数:intval(): 2.四舍五入取整:round(): 3.向上取整,有小数就加1:ceil(): 4.向下取整:floor(). ...

  2. 网站启用SSL后重启Nginx提示 Enter PEM Pass Phrase:需要输入密码

    ​最近在玩 STARTSSL 感觉个人站点使用这个SSL差不多也够用了.真正商用的SSL当然也可以自行购买. 我个人是为了防止数据中间被抓走,所以用了startssl 也基本就够用了. 转回正题,st ...

  3. mongoimport mongo导入Json的用法

    //导入json mongoimport --db ML_OER --collection lecture --file /home/tmp/course_temp.json //导入Json数组 m ...

  4. Atitit spring 3.0 3.1 3.2 4.0 4.3 5.0 新特性

    Atitit spring 3.0 3.1 3.2 4.0 4.3 5.0 新特性 Spring3.0的新特性及其分析 - 我的人生不甘于平庸! - ITeye技术网站.html Spring3.0带 ...

  5. 通过反射实现圆角ImageView

    private void init(){ paint = new Paint(Paint.ANTI_ALIAS_FLAG); roundRect = , , getWidth() , getHeigh ...

  6. golang bufio、ioutil读文件的速度比较(性能测试)和影响因素分析

    前言 golang读取文件的方式主要有4种: 使用File自带的Read方法 使用bufio库的Read方法 使用io/ioutil库的ReadAll() 使用io/ioutil库的ReadFile( ...

  7. centos 7 五笔安装

    # yum install ibus ibus-table-wubi 需要重启动

  8. [CoreOS 转载]CoreOS实践指南(二):架设CoreOS集群

    转载:http://www.csdn.net/article/2015-01-04/2823399 摘要:CoreOS是一个采用了高度精简的系统内核及外围定制的操作系统.ThoughtWorks的软件 ...

  9. 每日英语:Google Scraps Plan to Build Hong Kong Data Center

    Internet giant Google Inc. has scrapped a plan to build its own data center in Hong Kong and will in ...

  10. 显示eclipse中Problem窗口的方法

    https://blog.csdn.net/ningfuxuan/article/details/76395029 ****************************************** ...