#include <iostream>
using namespace std;
int main()
{
//1.const_cast
//const int a = 10;
//int* ptr = const_cast<int*>(&a);//将const属性去除
//*ptr = 100; //2.static_cast
//编译器隐式执行的操作都可以由static_cast显示完成,显示的强制类型转换会产生警告,当使用static_cast会关闭警告
//static_cast就是基本的类型转换
double dNum = 12.2356;
void* ptr = &dNum;
//double* dp = static_cast<double*>(ptr);//这个是c++标准的形式
double* dp = (double*)ptr;//c语言的强制转换,效果和上一句一样 //3.reinterpret_cast 我到底要看看这个是干啥的
//根据二进制位进行重新解释,看这个没啥意义吧 //4.dynamic_cast有时间再看一下
getchar();
return ;
} //一个区别static_cast和reinterpret_cast的例子
reinterpret_cast是为了映射到一个完全不同类型的意思,这个关键词在我们需要把类型映射回原有类型时用到它。我们映射到的类型仅仅是为了故弄玄虚和其他目的,这是所有映射中最危险的。(这句话是C++编程思想中的原话)
static_cast和reinterpret_cast的区别主要在于多重继承,比如
class A {
public:
int m_a;
}; class B {
public:
int m_b;
}; class C : public A, public B {};
那么对于以下代码:
C c;
printf("%p, %p, %p", &c, reinterpret_cast<B*>(&c), static_cast <B*>(&c));
前两个的输出值是相同的,最后一个则会在原基础上偏移4个字节,这是因为static_cast计算了父子类指针转换的偏移量,并将之转换到正确的地址(c里面有m_a,m_b,转换为B*指针后指到m_b处),而reinterpret_cast却不会做这一层转换。
因此, 你需要谨慎使用 reinterpret_cast.

staitc_cast,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 ...

随机推荐

  1. [TypeScript] Use the JavaScript “in” operator for automatic type inference in TypeScript

    Sometimes we might want to make a function more generic by having it accept a union of different typ ...

  2. 倍福TwinCAT(贝福Beckhoff)常见问题(FAQ)-人机界面快速入门 TC2

    创建最简单的静态文本,就像是label,就只需要绘制一个矩形框,然后填写Text,取消边框即可(你也可以设置自定义字体)   创建动态的文本框,就像是textbox,需要设置这个矩形框的Text为%d ...

  3. redis源代码分析(5)——aof

    前面几篇基本介绍了redis的主要功能.流程.接下来是一些相对独立的部分,首先看一下持久化. redis持久化支持两种方式:RDB和AOF,我们首先看一下AOF的实现. AOF(Append only ...

  4. ios 第三方qq登陆,号码禁止授权

    在以下加入測试账号就可以 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvYWxpbmNleGlhb2hhbw==/font/5a6L5L2T/fontsiz ...

  5. Spring 中bean的作用、定义

    Spring 中bean的作用.定义: 创建一个bean定义,其实质是用该bean定义对应的类来创建真正实例的"配方(recipe)".把bean定义看成一个配方很有意义,它与cl ...

  6. spring学习笔记(四)

    1.aop编程 a.前置通知 .... <!-- 配置被代理的对象 -->    <bean id="test1Service" class="com. ...

  7. poj 2601 Simple calculations

    Simple calculations Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6559   Accepted: 32 ...

  8. Mybatis学习-1(转自 csdn- http://my.csdn.net/hupanfeng 的文章)

    简介 MyBatis的前身叫iBatis,本是apache的一个开源项目, 2010年这个项目由apache software foundation 迁移到了google code,并且改名为MyBa ...

  9. QT学习二:工具栏

    QT  的工具栏提供 Dock 功能.文字/图标显示排列的功能. void MainWindow::createToolBar(void) { QToolBar *toolBar = new QToo ...

  10. Drawable资源的初步使用

    刚開始接触到Android的时候,看到类似以下的一个Button: 当时感觉这种button有点像Material Design风格.真的以为是裁剪好的图片,好奇心驱使我上网查找实现的方法,原来不是裁 ...