///////////////////////////////////////////////////////////////////////////////
//
// 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. [mysql]支持emoji(字符集问题)!

    问题的根源 主要问题就是在字符集,一般解决这种问题都是靠试验.我实验了一通,得出的结论和大家分享一下(如有错误,还望指正): 数据库的字符集 数据库连接的字符集 配置方法 设置数据库的字符集为utf8 ...

  2. heaters

    https://leetcode.com/problems/heaters/ 开始的时候,下面的代码对于两边数字完全一样的情况,测试不通过.原因是heater会有重复情况,这时候对于飘红部分就不会往前 ...

  3. ajax请求成功或失败的参数

    success:function(response, status, xhr){ }, error:function(xhr, errorText, errorType){ alert(errorTe ...

  4. linux 文件类型 文件权限

    linux中常见的文件类型有: “—”表示普通文件 :-rw-r--r-- 1 root root 41727 07-13 02:56 install.log   “d”表示目录 :drwxr-xr- ...

  5. 串行通讯之.NET SerialPort异步写数据

    目录 第1章说明    2 1 为什么需要异步写数据?    2 2 异步写数据的代码    2 3 源代码    4 第1章说明 1 为什么需要异步写数据? 如下图所示,以波特率300打开一个串口. ...

  6. 【转】 从最简单的vector中sort用法到自定义比较函数comp后对结构体排序的sort算法

    sort函数在使用中非常好用,也非常简单,而且效率与冒泡或者选择排序不是一个数量级.本文就sort函数在vector中的用法分为sort函数入门用法与自定义comp比较函数比较结构体这两个最基本的功能 ...

  7. webstorm搭建node服务器

    前言,搭建服务器,必须有node.js环境(吐槽:本来就是用node搭建的(⊙o⊙)…) 下载node.js 网址  https://nodejs.org/en/ 先新建项目: 这里选择Node.js ...

  8. CentOS报错:Could not retrieve mirrorlist http://mirrorlist.centos.org/?release=7&arch=x86_64&repo=os&infra=stock32 error was 14: curl#6 - "Could not resolve host: mirrorlist.centos.org; Unknown error"

    今天安装完带图形界面的CentOS 7后,在Terminal中运行yum安装命令时报了以下错误: Could not retrieve mirrorlist http://mirrorlist.cen ...

  9. python 第三方模块 转 https://github.com/masterpy/zwpy_lst

    Chardet,字符编码探测器,可以自动检测文本.网页.xml的编码. colorama,主要用来给文本添加各种颜色,并且非常简单易用. Prettytable,主要用于在终端或浏览器端构建格式化的输 ...

  10. HDUOJ--------A simple stone game(尼姆博弈扩展)(2008北京现场赛A题)

    A simple stone game                                                                                  ...