C++.运行时类型判断_测试代码
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++.运行时类型判断_测试代码的更多相关文章
- C++运行时类型判断dynamic_cast和typeid
dynamic_cast dynamic_cast < Type-id > ( expression ) dynamic_cast<类型>(变量) 在运行期间检测类型转换是否安 ...
- RTTI (Run-Time Type Identification,通过运行时类型识别) 转
参考一: RTTI(Run-Time Type Identification,通过运行时类型识别)程序能够使用基类的指针或引用来检查这些指针或引用所指的对象的实际派生类型. RTTI提供了以下两个 ...
- 8. 多态——编译时类型&运行时类型
一.引用变量的两种类型 1. 编译时类型:由声明该变量时使用的类型决定 2. 运行时类型:由实际赋给该变量的对象决定 如果编译时类型和运行时类型不一致,就可能出现多态. class BaseClass ...
- 【JavaSE】运行时类型信息(RTTI、反射)
运行时类型信息使得你可以在程序运行时发现和使用类型信息.--<Think in java 4th> **** 通常我们在面向对象的程序设计中我们经常使用多态特性使得大部分代码尽可能地少了解 ...
- MFC六大核心机制之二:运行时类型识别(RTTI)
上一节讲的是MFC六大核心机制之一:MFC程序的初始化,本节继续讲解MFC六大核心机制之二:运行时类型识别(RTTI). typeid运算子 运行时类型识别(RTTI)即是程序执行过程中知道某个对象属 ...
- c++运行时类型识别(rtti)
一个简单运行时类型识别 namespace rtti_ex { /* * 类型信息基类 */ class i_type_info { public: // 判断是否是指定类型 bool is(cons ...
- MFC原理第三讲.RTTI运行时类型识别
MFC原理第三讲.RTTI运行时类型识别 一丶什么是RTTI RTTI. 运行时的时候类型的识别. 运行时类型信息程序.能够使用基类(父类)指针 或者引用 来检查这些指针或者引用所指的对象. 实际派生 ...
- java多态的向上转型与向下转型(与编译时类型与运行时类型有关)
1.编译时类型由声明该变量时使用的类型决定,运行时类型由实际赋给该变量的对象决定. 当编译时类型和运行时类型不一致时,就会出现所谓的多态. 因为子类是一个特殊的父类,因此java允许把一个子类对象直接 ...
- 《深入浅出MFC》系列之运行时类型识别(RTTI)
/********************************************************************************** 发布日期:2017-11-13 ...
随机推荐
- Kattis之旅——Rational Arithmetic
Input The first line of input contains one integer, giving the number of operations to perform. Then ...
- Linux环境jdk的安装
1.下载jdk1.7,oracle的下载地址已经失效,找了个其他的地址进行下载. wget http://pc.xzstatic.com/2017/03/jdk7u79linuxx64.tar.gz ...
- Context initialization failed
Context initialization failed org.springframework.beans.factory.BeanDefinitionStoreException: Invali ...
- no matching editors or conversion strategy found
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionM ...
- uwsgi 的巨坑
网上各种找,最后自己猜,猜到了. 必须安装python插件, 网上找的都是不带数字的版本号, 要么找不到要么不行. 我是 3.6.1,尝试加36, 成了. yum install -y uwsgi-p ...
- Linux 安装本地 yum源
放入Centos6.4的镜像光盘或找到镜像文件 [root]#mount /dev/cdrom /media/cdrom #挂载本地镜像 [root]#rm -rf /etc/yum.repo.d ...
- 11: Nginx安装lua支持
1.1 Nginx 使用lua脚本 注:需要LuaJIT-2.0.4.tar.gz,ngx_devel_kit,lua-nginx-module 1.Nginx安装lua支持 wget -c http ...
- Eclipse中已安装的插件如何卸载
最近在Eclipse中安装了一个插件,导致Eclipse使用的时候有些问题,就找了资料,原来Eclipse中的插件也是可以卸载的. 方法是点击菜单“Help”,“Install New Softwar ...
- mint-ui之Swipe使用
<template> <div> <div class="swipe-wrapper"> <mt-swipe :auto="10 ...
- forever 用法
安装过Node.js后再安装forever,需要加-g参数,因为forever要求安装到全局环境下: npm install forever -g 常用指令: npm install forever ...