ZC:C++ 编程思想——运行时类型识别 - 浅墨浓香 - 博客园.html(https://www.cnblogs.com/5iedu/articles/5585895.html

------------------------------两种Bad-cast-----------------------------------
1. dynamic_cast转换一个完全不相关的类
2. typeid操作一个空指针

1、环境:Win7x64、Qt5.3.2 MSVC2010 OpenGL、vs2010

2、代码:

    class Tbase
{
public:
int Fi; public:
virtual void Say(){ qDebug() << "Tbase"; }
}; class Tother
{
public:
int Fi; public:
virtual void Say(){ qDebug() << "Tother"; }
}; class TA1 :public Tbase
{
public:
int FiA; public:
virtual void Say(){ qDebug() << "TA1"; }
}; class TB1 :public TA1
{
public:
int FiB; public:
virtual void Say(){ qDebug() << "TB1"; }
}; class TC1 :public TB1
{
public:
int FiC; public:
virtual void Say(){ qDebug() << "TC1"; }
}; class TA2 :public Tbase
{
public:
int FiA; public:
virtual void Say(){ qDebug() << "TA2"; }
}; class TB2 :public TA2
{
public:
int FiB; public:
virtual void Say(){ qDebug() << "TB2"; }
}; class TC2 :public TB2
{
public:
int FiC; public:
virtual void Say(){ qDebug() << "TC2"; }
}; void MainWindow::on_pushButton_clicked()
{
Tbase *pC1 = new TC1(); if ( typeid(*pC1) == typeid(TC1) ) { qDebug() << "pC1 is TC1"; } else { qDebug() << "pC1 is not TC1"; }  // ZC: is
if ( typeid(*pC1) == typeid(TB1) ) { qDebug() << "pC1 is TB1"; } else { qDebug() << "pC1 is not TB1"; }  // ZC: is not
if ( typeid(*pC1) == typeid(TA1) ) { qDebug() << "pC1 is TA1"; } else { qDebug() << "pC1 is not TA1"; }  // ZC: is not
if ( typeid(*pC1) == typeid(Tbase) ) { qDebug() << "pC1 is Tbase"; } else { qDebug() << "pC1 is not Tbase"; }  // ZC: is not
qDebug() << ""; if ( typeid(*pC1) == typeid(TC2) ) { qDebug() << "pC1 is TC2"; } else { qDebug() << "pC1 is not TC2"; }  // ZC: is not
if ( typeid(*pC1) == typeid(TB2) ) { qDebug() << "pC1 is TB2"; } else { qDebug() << "pC1 is not TB2"; }  // ZC: is not
if ( typeid(*pC1) == typeid(TA2) ) { qDebug() << "pC1 is TA2"; } else { qDebug() << "pC1 is not TA2"; }  // ZC: is not
qDebug() << ""; // *** Tbase *pB1 = new TB1(); if ( typeid(*pB1) == typeid(TC1) ) { qDebug() << "pB1 is TC1"; } else { qDebug() << "pB1 is not TC1"; }  // ZC: is not
if ( typeid(*pB1) == typeid(TB1) ) { qDebug() << "pB1 is TB1"; } else { qDebug() << "pB1 is not TB1"; }  // ZC: is
if ( typeid(*pB1) == typeid(TA1) ) { qDebug() << "pB1 is TA1"; } else { qDebug() << "pB1 is not TA1"; }  // ZC: is not
if ( typeid(*pB1) == typeid(Tbase) ) { qDebug() << "pB1 is Tbase"; } else { qDebug() << "pB1 is not Tbase"; }  // ZC: is not
qDebug() << ""; if ( typeid(*pB1) == typeid(TC2) ) { qDebug() << "pB1 is TC2"; } else { qDebug() << "pB1 is not TC2"; }  // ZC: is not
if ( typeid(*pB1) == typeid(TB2) ) { qDebug() << "pB1 is TB2"; } else { qDebug() << "pB1 is not TB2"; }  // ZC: is not
if ( typeid(*pB1) == typeid(TA2) ) { qDebug() << "pB1 is TA2"; } else { qDebug() << "pB1 is not TA2"; }  // ZC: is not
qDebug() << ""; qDebug() << "*** *** *** *** *** *** *** *** *** *** ***"; } void MainWindow::on_pushButton_2_clicked()
{
Tbase *pBase = new TC1();
TC1* pC1 = dynamic_cast<TC1*>(pBase);
TB1* pB1 = dynamic_cast<TB1*>(pBase);
TA1* pA1 = dynamic_cast<TA1*>(pBase);
qDebug() << "pC1" << (int)pC1;  // ZC: != 0
qDebug() << "pB1" << (int)pB1;  // ZC: != 0
qDebug() << "pA1" << (int)pA1;  // ZC: != 0 TC2* pC2 = dynamic_cast<TC2*>(pBase);
TB2* pB2 = dynamic_cast<TB2*>(pBase);
TA2* pA2 = dynamic_cast<TA2*>(pBase);
qDebug() << "pC2" << (int)pC2;  // ZC: == 0
qDebug() << "pB2" << (int)pB2;  // ZC: == 0
qDebug() << "pA2" << (int)pA2;  // ZC: == 0 qDebug() << ""; pBase = new TB1();
pC1 = dynamic_cast<TC1*>(pBase);
pB1 = dynamic_cast<TB1*>(pBase);
pA1 = dynamic_cast<TA1*>(pBase);
qDebug() << "pC1" << (int)pC1;  // ZC: == 0
qDebug() << "pB1" << (int)pB1;  // ZC: != 0
qDebug() << "pA1" << (int)pA1;  // ZC: != 0 pC2 = dynamic_cast<TC2*>(pBase);
pB2 = dynamic_cast<TB2*>(pBase);
pA2 = dynamic_cast<TA2*>(pBase);
qDebug() << "pC2" << (int)pC2;  // ZC: == 0
qDebug() << "pB2" << (int)pB2;  // ZC: == 0
qDebug() << "pA2" << (int)pA2;  // ZC: == 0 qDebug() << ""; pBase = new Tbase();
Tother* pOther = dynamic_cast<Tother*>(pBase);
qDebug() << "pOther" << (int)pOther;  // ZC: == 0 }

3、控制台输出:

  3.1、Debug:

  3.2、Release:

pC1 is TC1
pC1 is not TB1
pC1 is not TA1
pC1 is not Tbase pC1 is not TC2
pC1 is not TB2
pC1 is not TA2 pB1 is not TC1
pB1 is TB1
pB1 is not TA1
pB1 is not Tbase pB1 is not TC2
pB1 is not TB2
pB1 is not TA2 *** *** *** *** *** *** *** *** *** *** ***
pC1 4912920
pB1 4912920
pA1 4912920
pC2 0
pB2 0
pA2 0 pC1 0
pB1 4919680
pA1 4919680
pC2 0
pB2 0
pA2 0 pOther 0

4、想在 构造函数中 判断 自己是哪个类,为 TA1添加构造函数:

  

  编译的时候,直接就报错了...

5、

C++.运行时类型判断_测试代码的更多相关文章

  1. C++运行时类型判断dynamic_cast和typeid

    dynamic_cast dynamic_cast < Type-id > ( expression ) dynamic_cast<类型>(变量) 在运行期间检测类型转换是否安 ...

  2. RTTI (Run-Time Type Identification,通过运行时类型识别) 转

    参考一: RTTI(Run-Time Type Identification,通过运行时类型识别)程序能够使用基类的指针或引用来检查这些指针或引用所指的对象的实际派生类型.   RTTI提供了以下两个 ...

  3. 8. 多态——编译时类型&运行时类型

    一.引用变量的两种类型 1. 编译时类型:由声明该变量时使用的类型决定 2. 运行时类型:由实际赋给该变量的对象决定 如果编译时类型和运行时类型不一致,就可能出现多态. class BaseClass ...

  4. 【JavaSE】运行时类型信息(RTTI、反射)

    运行时类型信息使得你可以在程序运行时发现和使用类型信息.--<Think in java 4th> **** 通常我们在面向对象的程序设计中我们经常使用多态特性使得大部分代码尽可能地少了解 ...

  5. MFC六大核心机制之二:运行时类型识别(RTTI)

    上一节讲的是MFC六大核心机制之一:MFC程序的初始化,本节继续讲解MFC六大核心机制之二:运行时类型识别(RTTI). typeid运算子 运行时类型识别(RTTI)即是程序执行过程中知道某个对象属 ...

  6. c++运行时类型识别(rtti)

    一个简单运行时类型识别 namespace rtti_ex { /* * 类型信息基类 */ class i_type_info { public: // 判断是否是指定类型 bool is(cons ...

  7. MFC原理第三讲.RTTI运行时类型识别

    MFC原理第三讲.RTTI运行时类型识别 一丶什么是RTTI RTTI. 运行时的时候类型的识别. 运行时类型信息程序.能够使用基类(父类)指针 或者引用 来检查这些指针或者引用所指的对象. 实际派生 ...

  8. java多态的向上转型与向下转型(与编译时类型与运行时类型有关)

    1.编译时类型由声明该变量时使用的类型决定,运行时类型由实际赋给该变量的对象决定. 当编译时类型和运行时类型不一致时,就会出现所谓的多态. 因为子类是一个特殊的父类,因此java允许把一个子类对象直接 ...

  9. 《深入浅出MFC》系列之运行时类型识别(RTTI)

    /********************************************************************************** 发布日期:2017-11-13  ...

随机推荐

  1. sqlserver搜索中怎么把varchar类型转换成numeric类型

    sqlserver搜索中怎么把varchar类型转换成numeric类型 可以用cast来转换 如:列名叫grade,表名为A select cast(grade as numeric(y,x)) f ...

  2. Maven项目启动报错:org.springframework.web.filter.CharacterEncodingFilter cannot be cast to javax.servlet.Filter

    看网上说法tomcat启动时会把lib目录下的jar包加载进内存,而项目里也有相同的jar包就会导致jar包冲突 解决办法: 把pom依赖里相应的jar包添加<scope>标签 <d ...

  3. 全球最大的3D数据集公开了!标记好的10800张全景图

    Middlebury数据集 http://vision.middlebury.edu/stereo/data/ KITTI数据集简介与使用 https://blog.csdn.net/solomon1 ...

  4. OpenGL读取帧缓存数据

    https://blog.csdn.net/niu2212035673/article/details/80251949 简述有些时候我们可能需要获取渲染后的图像数据,比较常用的函数是glReadPi ...

  5. li设置inline-block后,li左边出现空隙问题。

    方法1:在ul设置font-size=0,然后再li再单独设置font-size 方法2:li连着写不要换行,也可以解决. <ul> <li>测试1</li>< ...

  6. imageio.ffmpeg.download() has been deprecated. Use 'pip install im ageio-ffmpeg' instead.'

    Use this instead: sudo pip3 install imageio==2.4.1

  7. VIM编码检查

    trouble shooting https://www.django.cn/article/show-4.html https://blog.csdn.net/lh756437907/article ...

  8. Redis Desktop Manager连接Redis

    1.注释redis.conf文件中的:bind 127.0.0.1修改为自己的IP 2.ifconfig查看自己的虚拟机ip 3.拿到IP后,返回Windows,开启cmd,通过telnet命令,测试 ...

  9. 【题解】Luogu P4679 [ZJOI2011]道馆之战

    原题传送门 码农题树剖好题,口袋妖怪是个好玩的游戏 这道题要用树链剖分,我博客里有对树链剖分的详细介绍 下文左右就代表树的节点按dfs序后的左右,上.下分别表示每个节点的A.B区域 考虑在链上的情况, ...

  10. 图片上传前 压缩,base64图片压缩 Exif.js处理ios拍照倒置等问题

    曾写过在前端把图片按比例压缩不失真上传服务器的前端和后台,可惜没有及时做总结保留代码,只记得js利用了base64位压缩和Exif.js进行图片处理,还有其中让我头疼的ios拍照上传后会倒置等诸多问题 ...