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. linux下VLAN设置

    1. 安装vlan(vconfig)和加载8021q模块 [root@test0001~]#yum install vconfig [root@test0001~]#modprobe 8021q [r ...

  2. How many zero's and how many digits ? UVA - 10061

    Given a decimal integer number you will have to find out how many trailing zeros will be there in it ...

  3. 深度估计&平面检测小结

    https://yq.aliyun.com/ziliao/582885 最近一段时间已知忙着赶图像分析与理解的项目,在三个星期内强行接触了CNN,MRF,Caffe,openCV在内的很多东西.现在项 ...

  4. MySQL半同步安装以及参数

    MySQL半同步安装以及参数 基于MySQL5.5 官档地址: Semisynchronous Replication Administrative Interface https://dev.mys ...

  5. css overflow和float

    float:使元素向左或向右移动(不能上下移动),直到它的外边缘碰到包含框或另一个浮动框的边框为止,浮动元素之前的元素将不会受到影响,之后的元素将围绕它. float之后的元素脱离文档流. 默认为no ...

  6. Builgen 插件——IntelliJ IDEA和Eclipse Java Bean Builder模式代码生成器-比lombok更符合需求

    builder模式在越来越多的项目中使用,类似于alibaba fastjson JSONObject.fluentPut(),调用一个方法后返回这个对象本身,特别适合构建一些参数超级多的对象,代码优 ...

  7. 模拟实现ATM+购物商城程序

    流程图: 需求: ATM:模拟实现一个ATM + 购物商城程序额度 15000或自定义实现购物商城,买东西加入 购物车,调用信用卡接口结账可以提现,手续费5%支持多账户登录支持账户间转账记录每月日常消 ...

  8. Bugku-CTF之矛盾

    Day5 地址:http://123.206.87.240:8002/get/index1.php      

  9. Starting MySQL ERROR! Couldn't find MySQL server (/usr/local/mysql/bin/mysqld_safe)

    centos7.5 安装mysql数据库报错 问题: [root@db04-54 scripts]# /etc/init.d/mysqld start /etc/init.d/mysqld: line ...

  10. P2057 [SHOI2007]善意的投票

    思路 简单的最小割模型 最小割的模型就是选出一些边,把点集划分成S和T两个部分,使得代价最小 到这题上就是板子了 代码 #include <cstdio> #include <alg ...