In C++, RTTI (Run-time type information) is available only for the classes which have at least one virtual function.

  For example, dynamic_cast uses RTTI and following program fails with error “cannot dynamic_cast `b’ (of type `class B*’) to type `class D*’ (source type is not polymorphic) ” because there is no virtual function in the base class B.

 1 #include<iostream>
2
3 using namespace std;
4
5 class B
6 {
7 };
8 class D: public B
9 {
10 };
11
12 int main()
13 {
14 B *b = new D;
15 D *d = dynamic_cast<D*>(b);
16 if(d != NULL)
17 {
18 cout<<"works";
19 }
20 else
21 {
22 cout<<"cannot cast B* to D*";
23 }
24 getchar();
25 return 0;
26 }

  Adding a virtual function to the base class B makes it working.

 1 #include<iostream>
2
3 using namespace std;
4
5 class B
6 {
7 public:
8 virtual void fun()
9 {
10 }
11 };
12 class D: public B
13 {
14 };
15
16 int main()
17 {
18 B *b = new D;
19 D *d = dynamic_cast<D*>(b);
20 if(d != NULL)
21 {
22 cout<<"works";
23 }
24 else
25 {
26 cout<<"cannot cast B* to D*";
27 }
28 getchar();
29 return 0;
30 }

  

  Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

  转载请注明:http://www.cnblogs.com/iloveyouforever/

  2013-11-26  21:13:16

RTTI (Run-time type information) in C++的更多相关文章

  1. RTTI(Runtime Type Information )

    RTTI 是“Runtime Type Information”的缩写,意思是:运行时类型信息.它提供了运行时确定对象类型的方法.本文将简略介绍 RTTI 的一些背景知识.描述 RTTI 的概念,并通 ...

  2. C++ - RTTI(RunTime Type Information)执行时类型信息 具体解释

    RTTI(RunTime Type Information)执行时类型信息 具体解释 本文地址: http://blog.csdn.net/caroline_wendy/article/details ...

  3. Thinking in Java,Fourth Edition(Java 编程思想,第四版)学习笔记(十四)之Type Information

    Runtime type information (RTTI) allow you to discover and use type information while a program is ru ...

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

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

  5. Dynamic type checking and runtime type information

    动态类型的关键是将动态对象与实际类型信息绑定. See also: Dynamic programming language and Interpreted language Dynamic type ...

  6. TIJ——Chapter Fourteen:Type Information

    Runtime type information(RTTI) allows you to discover and use type information while a program is ru ...

  7. (TODO:)下载图片,报错:warning: could not load any Objective-C class information from the dyld shared cache. This will significantly reduce the quality of type information available.

    想使用NSInvocationOperation下载图片,然而并没有下载下来, NSData为nil, 还有报错:(打断点就报错) warning: could not load any Object ...

  8. TIJ学习--RTTI(Runtime-Time Type Identification)

    TIJ学习--RTTI(Runtime-Time Type Identification) RTTI 运行时类型检查机制 获取一个类的Class引用的三种方法 class TestClass{} Te ...

  9. c++对象模型和RTTI(runtime type information)

    在前面已经探讨过了虚继承对类的大小的影响,这次来加上虚函数和虚继承对类的大小的影响. 先来回顾一下之前例子的代码: #include <iostream> using namespace ...

随机推荐

  1. 为何我中断执行的线程不起作用,Why

    摘要:我们就以一个案例的形式,来为大家详细介绍下为何中断执行的线程不起作用. 本文分享自华为云社区<明明中断了线程,却为何不起作用呢?>,作者:冰 河. 当我们在调用Java对象的wait ...

  2. docker添加sudo权限

    sudo groupadd docker  # 添加group sudo gpasswd -a think docker  # 添加用户到组 sudo service docker restart n ...

  3. FastApi下载文件

    FastApi下载文件 记得之前我们讲过生成excel文件的事情,那么如何把服务器生成的excel文件正确发送给用户呢? 今天我们就来说说在FastApi中如何正确让用户下载到想要的文件. 基本流程 ...

  4. newInstance方法

    1.new 是java中的关键字,是创建一个新对象的关键字.用new这个关键字的话,是调用new指令创建一个对象,然后调用构造方法来初始化这个对象,如果反编译class的话,会看到一个Object o ...

  5. 《Python语言程序设计》【第3周】基本数据类型

    实例3:天天向上的力量 #DayDayUpQ1.py dayup = pow(1.001,365) daydown = pow(0.999,365) print("向上: {:.2f},向下 ...

  6. 如何使用原生的Hystrix

    什么是Hystrix 前面已经讲完了 Feign 和 Ribbon,今天我们来研究 Netflix 团队开发的另一个类库--Hystrix. 从抽象层面看,Hystrix 是一个保护器.它可以保护我们 ...

  7. Serverless 下的微服务实践

    作者:弈川 审核&校对:筱姜.潇航 编辑&排版:雯燕 微服务架构介绍 微服务架构诞生背景 在互联网早期即 Web 1.0 的时代,当时流行的是单体应用,研发团队比较小,主要是外部网页, ...

  8. Java包装类,以及Integer与int之间的比较

    一.Java的基本类型 Java语言中提供了八种基本类型,包括六种数字类型(四个整数型,两个浮点型),一种字符类型,还有一种布尔型. 整数型,包括byte.short.int.long,默认初始值是0 ...

  9. [loj6033]棋盘游戏

    将棋盘黑白染色,即构成一张二分图 将状态用一张二分图$G$和一个点$x\in V$描述(分别为仍未被经过的点的导出子图和当前棋子所在位置),并称将要移动棋子的一方为先手 结论:先手必胜当且仅当$x$一 ...

  10. SpringCloud微服务实战——搭建企业级开发框架(二十一):基于RBAC模型的系统权限设计

    RBAC(基于角色的权限控制)模型的核心是在用户和权限之间引入了角色的概念.取消了用户和权限的直接关联,改为通过用户关联角色.角色关联权限的方法来间接地赋予用户权限,从而达到用户和权限解耦的目的. R ...