使用reinterpret_cast的危险
关键字: c++ cast
// Cast.cpp : Defines the entry point for the console application.
// #include "stdafx.h"
#include <iostream> using namespace std; class Base
{
public: virtual void func1() = ;
virtual void func2() = ;
}; class A
{
virtual void func3();
}; void A::func3()
{
cout<<"func3"<<endl;
} class B : public A, public Base
{
public:
virtual void func1();
virtual void func2();
}; void B::func1()
{
cout<<"func1"<<endl;
} void B::func2()
{
cout<<"func2"<<endl;
} void DynamicCast()
{
A* pA = new B();
Base* pBase = dynamic_cast<Base*>(pA);//Work fine
pBase->func1();
pBase->func2();
} void StaticCast()
{
A* pA = new B();
//Base* pBase = static_cast<Base*>(pA);//Compiler error
//pBase->func1();
//pBase->func2();
} void ReinterpretCast()
{
A* pA = new B();
Base* pBase = reinterpret_cast<Base*>(pA);
pBase->func1();
pBase->func2();//run-time error: Access violation
} int _tmain(int argc, _TCHAR* argv[])
{
DynamicCast();
ReinterpretCast(); return ;
}
使用reinterpret_cast的危险的更多相关文章
- C++易混淆知识点整理
// 1 /////////////////////////////////////////////////////////////////////// // 常量指针:,指针可修改,变量不可修改(只 ...
- c++第二十九天
p143~p151:其他隐式类型转换1.数组转换成指针,大多数表达式自动转换成指向数组首元素的指针. 2.指针的转换. 3.转换成布尔类型,例如在if (condition) 中. 4.转换成常量. ...
- C++中的显示类型转换
本文参考了<C++ Primer(中文 第5版)>.<王道程序员求职宝典>以及网上相关博客,结合自己的理解写成.个人水平有限,若有错误欢迎指出. C++中显示转换也成为强制类型 ...
- C++ FAQ
空类 class A { }; // sizeof(A) = 1 空类的大小之所以为1,因为标准规定完整对象的大小>0,否则两个不同对象可能拥有相同的地址,故编译器会生成1B占位符. 那么两个对 ...
- C++标准转换运算符reinterpret_cast
C++标准转换运算符reinterpret_cast reinterpret_cast <new_type> (expression) reinterpret_cast运算符是用来处理无关 ...
- c++强制类型转换:dynamic_cast、const_cast 、static_cast、reinterpret_cast
c++强制类型转换:dynamic_cast.const_cast .static_cast.reinterpret_cast 博客分类: C/C++ CC++C#编程数据结构 dynamic_ca ...
- static_cast dynamic_cast const_cast reinterpret_cast总结对比
[本文链接] http://www.cnblogs.com/hellogiser/p/static_cast-dynamic_cast-const_cast-reinterpret_cast.html ...
- c++强制类型转换(static_cast,const_cast,dynamic_cast,reinterpret_cast)
static_cast <typeid>(exdlvssion) static_cast 很像 C 语言中的旧式类型转换.它能进行基础类型之间的转换,也能将带有可被单参调用的构造函数或用户 ...
- c++中的强制转换static_cast、dynamic_cast、reinterpret_cast的不同用法儿
c++中的强制转换static_cast.dynamic_cast.reinterpret_cast的不同用法儿 虽然const_cast是用来去除变量的const限定,但是static_cast ...
随机推荐
- java基础之synchronized使用方法
首先.參考文章:http://www.cnblogs.com/devinzhang/archive/2011/12/14/2287675.html PS:參考文章非常长,但内容非常丰富,若是刚開始学习 ...
- Android两个控件叠在一起,如何让被挡住的控件显示出来
Android两个控件叠在一起,如何让被挡住的控件显示出来 问题 : 两个控件叠在一起,如何让被挡住的控件显示出来? 比如A,B两个控件,A被B挡住,目前A要显示出来,B不能被隐藏,A的高度只有那么一 ...
- 在TextView使用部分颜色文字
/** * change a part of string color. * * @param string * whole string. * @param subString * the sub ...
- careercup-递归和动态规划 9.11
9.11 给定一个布尔表达式,由0.1.&.|和^等符号组成,以及一个想要的布尔结果result,实现一个函数,算出有几种括号的放法可使该表达式得出result值. 解法: 跟其他递归问题一样 ...
- cocos2d-x 开发中的小问题 在xcode4环境下
转自:http://hi.baidu.com/baby_66_/item/302353174f19521cd0d66df2 1.如果你在想怎么去搞定程序的开始运行的背景一闪而过的大图 以及icon想换 ...
- Android开发之Bitmap二次采样
Bitmap二次采样,听着好像是一个高大上的事,其实也就那么回事,今天我们就来看看Bitmap的二次采样问题. 1.为什么要二次采样 OK,那么首先我要 解决的一个问题就是为什么我们要二次采样? 不知 ...
- JNI 学习笔记系列(一)
JNI全称是Java native interface,它是一个中间件,通过JNI可以使Java和C语言之间互相调用,在android开发中,像wifi热点的开启,像极品飞车中重力加速,碰撞效果的模拟 ...
- 在文件地理数据库中使用 SQL 进行报告和分析 (转)
================以下摘自ArcGIS10.1帮助=================== 文件地理数据库允许在 QueryDef 中通过 SubFields(字段列表)方法使用表达式和别 ...
- ASP与ASP.NET转换Session数据桥的应用
背景: 现有公司的产品OA是采用ASP早先的技术开发,需要与目前最新的ASP.NET产品进行数据交互的应用.现有的ASP应用程序往往采用“ASP Sessions”,这是一种经典的ASP内置模式,即允 ...
- TCP调试助手
网络开发经常要用到一些TCP&UDP的调试工具,搜集一些备用. 目前总结工具有(不分先后): chrome等自带调试器调试HTTP Fiddler(.NET)和Charles debugger ...