///////////////////////////////////////////////////////////////////////////////
//
// FileName : cast_item27.cpp
// Version : 0.10
// Author : Ryan Han
// Date : 2013/10/31 15:43:55
// Comment :
//
///////////////////////////////////////////////////////////////////////////////
#include <iostream>
using namespace std; int main() {
//static_cast
int a = ;
a = ;
const int b = static_cast<const int>(a);
cout << "b is: " << b << endl; //dynamic_cast
//see D:\cygwin\home\baoweih\code_book\c++ primer\p838_dynamiccast.h //reinterpret_cast
char* k = reinterpret_cast<char*>(b); //const_cast
//http://www.cnblogs.com/dracohan/p/3417842.html
const int* i = &b;
//compile error, invalid conversion from ¡®const int*¡¯ to ¡®int*¡¯
//int* j = i;
int* j = const_cast<int*>(i);
//successfully change a const value
*j = ;
cout << "b is: " << b << endl; //reference
const int& l = b;
int& m = const_cast<int&>(l);
//error: assignment of read-only reference ¡®l¡¯
//l = 7;
//successfully change a const reference
m = ;
cout << "b is: " << b << endl; //const point
const int* const pint = new int();
// can't change *pint
//*pint = 1023;
// cant' change pint also
//pint = new int(1023);
int* const fake_pint = const_cast<int* const>(pint);
// can change *pint now
*fake_pint = ;
// still cant' change pint also
//fake_pint = new int(1023); cout << "*fake_pint is: " << *fake_pint << endl; return ;
}

C++-const_cast, reinterpret_cast, static_cast的用法的更多相关文章

  1. dynamic_cast,const_cast,static_cast,reinterpret_cast 详解

    如果直接指针直接强转,将只能访问虚函数的内容,而不能访问特定类中的特定成员或方法!!!! 强制类型转换运算符:C++有四种强制类型转换符,分别是dynamic_cast,const_cast,stat ...

  2. C++中四种类型转换方式(ynamic_cast,const_cast,static_cast,reinterpret_cast)

    Q:什么是C风格转换?什么是static_cast, dynamic_cast 以及 reinterpret_cast?区别是什么?为什么要注意? A:转换的含义是通过改变一个变量的类型为别的类型从而 ...

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

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

  4. 四种强制类型转换的总结(const_cast、static_cast、dynamic_cast、reinterpreter_cast)

    四种强制类型转换的总结(const_cast.static_cast.dynamic_cast.reinterpreter_cast) 转载 2011年10月03日 23:59:05 标签: stru ...

  5. static_cast dynamic_cast const_cast reinterpret_cast总结对比

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

  6. C++提供的四种新式转换--const_cast dynamic_cast reinterpret_cast static_cast

    关于强制类型转换的问题,许多书都讨论过,写的最具体的是C++之父的<C++的设计和演化>. 最好的解决方法就是不要使用C风格的强制类型转换,而是使用标准C++的类型转换符:static_c ...

  7. const_cast, dynamic_cast, static_cast,reinterpret_cast

    一.const_cast:用于移除const数据,目标数据类型必须与原类型相同 二.dynamic_cast:用于在两个不同类型之间进行强制转换并且在执行运行时检查它.保证它的合法性,如果在两个互相矛 ...

  8. C++中reinterpret_cast、const_cast、static_cast、dynamic_cast的作用与区别

    1.reinterpret_cast 作用及原理:将一个类型的指针,转换为另一个类型的指针,这种转换不用修改指针变量值数据存放格式(不改变指针变量值),只需在编译时重新解释指针的类型就可以,当然他也可 ...

  9. C++的四种转换(const_cast、static_cast、dynamic_cast、reinterpreter_cast)

    static_cast 相当于C语言中的强制转换:(类型)表达式或类型(表达式),用于各种隐式转换 非const转const.void*转指针.int和char相互转换 用于基类和子类之间的指针和引用 ...

随机推荐

  1. 【Todo】Lucene系统学习

    之前已经写过一篇关于Lucene安装学习的文章:http://www.cnblogs.com/charlesblc/p/5980525.html 还有一篇关于Solr安装使用的文章:http://ww ...

  2. 【Todo】Java要学的一些比较好的框架和系统

    commons-pool apache的通用线程池 可以看看这篇文章:<使用common-pool实现的一个简单的线程池> Jmeter 性能测试 Squid Link

  3. 【Todo】秒杀系统材料

    秒杀系统:Link <一个经验证可落地的秒杀系统实践思路> 主要依赖于Redis进行处理. http://geek.csdn.net/news/detail/59847   淘宝大秒系统设 ...

  4. 由《win32多线程程序设计》临界区的问题所想

    之前看侯捷翻译的<win32多线程程序设计>中关于线程同步中的临界区问题,其中举得例子是对链表的操作.死锁的问题是对一个Swaplist函数的问题,现列举代码如下: void SwapLi ...

  5. gevent中如何实现长轮询

    浏览网页时,浏览器会传HTTP 请求到服务器,服务器会根据请求将网页的内容传给浏览器,但是在很多的情况下,使用者会需要看到最新的即时性资讯,例如观看股票市场行情,而在以前只能靠着重新载入网页才能获得最 ...

  6. 图像fft和wavelet变换矩阵和向量区别 dwt2和wavedec2联系

    1.  对于小波变换,dwt2 :单级离散2维小波变换 wavedec2 :多级2-D小波分解 matlab中这两者联系是都能对图像进行小波分解,区别是dwt2是二维单尺度小波变换,只能对输入矩阵X一 ...

  7. ios 修改导航条返回按钮

    ios 修改导航条返回按钮 方式一:使用系统的:可以更改系统的文字:以及通过设置导航条的颜色来达到预期的效果 UIBarButtonItem *backBtns = [[UIBarButtonItem ...

  8. Android Support Library控件详细介绍之RecyclerView

    RecyclerView控件 依赖  compile 'com.android.support:recyclerview-v7:24.1.1'RecyclerView也是容器控件,大多数的效果显示可通 ...

  9. Ubuntu下解决bash 没有那个文件或目录的方法

    因为之前电脑硬盘坏掉,维修换了新硬盘,今天重新安装了ubuntu,装好之后就赶紧搭建工作环境,将备份的资料拷贝进来,搭建交叉编译环境,但是发现,修改bashrc中PATH绝对路径指向交叉编译器后,在命 ...

  10. Rectangle Intersection Test (with C#)

    Rectangle Intersection Test (with C#) by Sebastian Krysmanskihttp://manski.net/2011/05/rectangle-int ...