使用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 ...
随机推荐
- 把DataTable 转换成Json格式,适用于EasyUI 绑定DataGrid
本文转载:http://www.cnblogs.com/liang--liang/archive/2013/02/05/2893030.html public static string DataTa ...
- linux-多线程
一.什么是线程? 线程是进程的一个实体,是CPU调度和分派的基本单位,它是比进程更小的能独立执行的基本单位.线程自己基本上不拥有系统资源,仅仅拥有一点在执行中不可缺少的资源(如程序计数器,一组寄存器和 ...
- android91 代码注册广播接收者
Activity: package com.itheima.register; import android.os.Bundle; import android.app.Activity; impor ...
- mysql_config_editor
加入账号: [root@server-mysql bin]# ./mysql_config_editor set --login-path=client --user=root --password ...
- Mysql权限对照表
Mysql分为5种数据权限.12种结构权限.11种管理权限,当我们需要精细化权限的时候,我们就需要对照来进行授权,这样可以很好的的控制登录人员的权限.
- Python之路【第二十一篇】:Django之Form组件
Django之Form组件 Django的Form主要具有一下几大功能: 生成HTML标签 验证用户数据(显示错误信息) HTML Form提交保留上次提交数据 初始化页面显示内容 小试牛刀 1. ...
- 使用 Date 和 SimpleDateFormat 类表示时间
在程序开发中,经常需要处理日期和时间的相关数据,此时我们可以使用 java.util 包中的Date类.这个类最主要的作用就是获取当前时间,我们来看下Date的类的使用: Date d=new Dat ...
- Installation error INSTALL_FAILED_VERSION_DOWNGRADE错误
最近折腾了一下Robotium自动化测试框架,发现问题还挺多,刚刚解决了一个问题,总算是把环境搞定了,可是一运行测试用例,发现又报Installation error INSTALL_FAILED_V ...
- 如何在VC++ 中调试MEX文件
MEX文件对应的是将C/C++文件语言的编写之后 得到的相关文件加载到Matlab中运行的一种方式, 现对于Matlab 中的某些程序运行效率而言, C/C++ 代码某些算法的领域上面执行效率很高,若 ...
- ios Objective-C的动态特性
这是一篇译文,原文在此,上一篇文章就是受这篇文章启发,这次干脆都翻译过来. 过去的几年中涌现了大量的Objective-C开发者.有些是从动态语言转过来的,比如Ruby或Python,有些是从强类型语 ...