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 ...
随机推荐
- puts函数
1.puts函数是gets函数的输出版本,它把指定的字符串写到标准输出并在末尾添加一个换行符 #include <stdio.h> #include <stdlib.h> in ...
- Python - mouse clicks and keystrokes on a background window
https://stackoverflow.com/questions/43965437/python-mouse-clicks-and-keystrokes-on-a-background-wind ...
- Angular4.x 安装|创建项目|目录结构|创建组件
Angular4.x 安装|创建项目|目录结构|创建组件 安装最新版本的 nodejs node.js 官网:https://nodejs.org/zh-cn/ 去官网下载 node.js,下一步下一 ...
- TF-IDF原理与实现
TF-IDF 原理与实现 目录 1.原理 2.伪代码 3.实现 1.原理 \[ TF-IDF = tf_{t,d} \times idf_{t}\\ tf_{t,d} = \frac{术语t在文档d中 ...
- kali linux 数据库分析工具简述
bbqsql SQL盲注可能很难被利用. 当可用的工具工作时,它们运行良好,但是当它们不工作时,您必须编写自定义的东西. 这是耗时且乏味的. BBQSQL可以帮助你解决这些问题. BBQSQL是一个用 ...
- Codeforces 799D Field expansion - 搜索 - 贪心
In one of the games Arkady is fond of the game process happens on a rectangular field. In the game p ...
- python之udp
import socket s = socket.socket(socket.AF_INET,socket.SOCK_DGRAM) s.bind(('127.0.0.1',8888)) while T ...
- django基础 -- 3. urls.py view.py 参数 别名 重定向 常用方法 静态文件
一.基本格式 from django.conf.urls import url from . import views #循环urlpatterns,找到对应的函数执行,匹配上一个路径就找到对应的函数 ...
- HDU - 1875 畅通工程再续【最小生成树】
Problem Description 相信大家都听说一个"百岛湖"的地方吧,百岛湖的居民生活在不同的小岛中,当他们想去其他的小岛时都要通过划小船来实现.现在政府决定大力发展百岛湖 ...
- thinkphp如何省略index.php
省略index.php叫做 伪静态化; 共有四个步骤: MariaDB[(none)]: 表示, 目前没有选择/使用 任何数据库. 如果use了数据库, 会提示: MariaDB[mysql]... ...