使用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 ...
随机推荐
- C++中的类访问控制
C++中 public,protected, private 访问标号小结 第一:private, public, protected 访问标号的访问范围. private:只能由1.该类中的函数.2 ...
- shell if判断(曾经被一个字符串相等的判断纠结半小时,最后只是if后少了个空格!) 和 awk引用外部变量判断
一.if判断 数字: $A=12 $B=15 if(("$A"<"$B")) if(("$A"=="$B")) 字 ...
- oracle数据库导入导出的dmp(转)
window下: imp必须要dba用户,所以用sysdba用户登陆,然后给予chnlmgr用户dba权限 grant connect,resource,dba to chnlmgr; 全部导入 im ...
- mysql init_connect 参数的其他用处
http://blog.itpub.net/133735/viewspace-691196/ init_connect 是可以动态在线调整的,这样就有了一些其他的用处 经过测试init_conne ...
- VC++深入详解-第二章学习心得
由于之前看过C++的一些知识,所以这一章就挑一点以前比较不太在意的进行记录 首先是虚函数,纯虚函数 虚函数用关键字virtual申明,我理解成为派生类提供覆盖 纯虚函数virtual xxx()=xx ...
- Bash For Loop Examples for Your Linux Shell Scripting--ref
There are two types of bash for loops available. One using the “in” keyword with list of values, ano ...
- android 中在CMD中查看sqlite
今天第一次学习Sqlite,在代码中执行了sql,但是不知道在上面地方才能直观的查看sqlite中的数据,下面是查看资料后的找到的查看方法: 通过上述可以从cmd进入数据库查看原文地址:http:// ...
- 用友U8按BOM计算销售订单物料需求SQL代码 第一稿
drop table #tmp1999 drop table #tmp2999 drop table #tmp3999 drop table #tmp4999 drop table #tmp5999 ...
- 记录下sublime text快捷方式
不得不说sublime text用过之后,爱不释手,这里收集一下常用的快捷方式: ctrl+shift+p:调出命令面板,在输入ss可以改变当前的代码的渲染和提示效果, 用起sublime text ...
- PHP中刷新输出缓冲
http://www.cnblogs.com/mutuan/archive/2012/03/18/2404957.html PHP中刷新输出缓冲buffer是一个内存地址空间,Linux系统默认大小一 ...