关键字: 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的危险的更多相关文章

  1. C++易混淆知识点整理

    // 1 /////////////////////////////////////////////////////////////////////// // 常量指针:,指针可修改,变量不可修改(只 ...

  2. c++第二十九天

    p143~p151:其他隐式类型转换1.数组转换成指针,大多数表达式自动转换成指向数组首元素的指针. 2.指针的转换. 3.转换成布尔类型,例如在if (condition) 中. 4.转换成常量. ...

  3. C++中的显示类型转换

    本文参考了<C++ Primer(中文 第5版)>.<王道程序员求职宝典>以及网上相关博客,结合自己的理解写成.个人水平有限,若有错误欢迎指出. C++中显示转换也成为强制类型 ...

  4. C++ FAQ

    空类 class A { }; // sizeof(A) = 1 空类的大小之所以为1,因为标准规定完整对象的大小>0,否则两个不同对象可能拥有相同的地址,故编译器会生成1B占位符. 那么两个对 ...

  5. C++标准转换运算符reinterpret_cast

    C++标准转换运算符reinterpret_cast reinterpret_cast <new_type> (expression) reinterpret_cast运算符是用来处理无关 ...

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

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

  7. static_cast dynamic_cast const_cast reinterpret_cast总结对比

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

  8. c++强制类型转换(static_cast,const_cast,dynamic_cast,reinterpret_cast)

    static_cast <typeid>(exdlvssion) static_cast 很像 C 语言中的旧式类型转换.它能进行基础类型之间的转换,也能将带有可被单参调用的构造函数或用户 ...

  9. c++中的强制转换static_cast、dynamic_cast、reinterpret_cast的不同用法儿

    c++中的强制转换static_cast.dynamic_cast.reinterpret_cast的不同用法儿   虽然const_cast是用来去除变量的const限定,但是static_cast ...

随机推荐

  1. ASM集群文件系统ACFS(ASM Cluster File System)

    在11g R2中ASM文件支持包括数据文件,控制文件,归档日志文件,spfile,RMAN备份文件,Change Tracking文件,数据泵Dump文件盒OCR文件等.而推出的ACFS和Oracle ...

  2. QTP下载链接

    QTP下载链接 QTP官网下载:http://www8.hp.com/us/en/software-solutions/software.html?compURI=1172957#.UNMOQ2_FW ...

  3. 教你用笔记本破解无线路由器password

    近期非常多人问我怎么破解WiFipassword…看来大家都对免费的东西比較有兴趣.要么也可能是我太招摇了…囧… 好吧,我就写篇小小的教程,看完后,你应该可以破解大部分无线路由器password了,写 ...

  4. arcgis10 安装1721错误

    arcgis10 安装1721错误,主要是ArcGIS License Manager 服务程序的位置不对,注册表lmgrd.exe中修改lmgrd.ex

  5. JAVA_基础面试题

    1.面向对象的特征有哪些方面   1.抽象:抽象就是忽略一个主题中与当前目标无关的那些方面,以便更充分地注意与当前目标有关的方面.抽象并不打算了解全部问题,而只是选择其中的一部分,暂时不用部分细节.抽 ...

  6. 安装完Kali linux 之后 需要配置的一些 常用软件 记录

    apt 阿里云开源镜像站 deb http://mirrors.aliyun.com/kali sana main non-free contribdeb http://mirrors.aliyun. ...

  7. 理解Objective-c中的copy

    说一下深拷贝和浅拷贝的基本概念:a指针指向地址A1, 浅拷贝是创建了一个b指针指向地址A1:深拷贝是创建了一个c指针指向地址A2,A1和A2的地址不同. 我们看到NSObject接口里面是已经声明了c ...

  8. C++学习(五)

    一.拷贝构造函数和拷贝赋值运算符1.拷贝构造:用一个已有的对象,构造和它同类型的副本对象——克隆.2.形如class X {  X (const X& that) { ... }};的构造函数 ...

  9. Excel操作之 导出生成多个sheet页面

    首先需要下载一个NPOI.dll 下载地址:http://download.csdn.net/detail/president810/9503038 using System; using Syste ...

  10. ibatis调存储过程返回游标

    http://blog.sina.com.cn/s/blog_6f3ca78f01010pmj.html iBatic调用与JAVA调用很类似,只是JAVA把参数的注册放到了类里面,而iBatis把参 ...