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. Loto实践干货(8) 实测 保险丝 用示波器带电流探头

    本文用LOTO示波器和5A的电流探头来实验两种常见类型的保险丝的保护曲线.一种是熔断型的,另一种是自恢复型的.我们通常需要在一些电路中对电流过大的情况做保护,比如防止用户把输出源短路,比如防止用户对电 ...

  2. 五(二)、spring 声明式事务xml配置

    概述: 接着上一节内容,把注解配置@@Transactional形式改为xml配置形式: 一.配置步骤 1.配置事务管理器 1 <!-- 1配置事务管理器 --> 2 <bean i ...

  3. Mysql教程:(二)分组与函数查询group by

    分组与函数查询 温馨提示:分组之后查询其他函数结果是不正确的: 分组函数:group by 按班级分组,查询出每班数学最高分:select class,max(maths) from score gr ...

  4. Vue的第一课

    终于学习到Vue了,美滋滋,给自己点个赞 前后端作用: 1.1vs1(一个Vue对象控制一个) <body> <div id="app"> <p> ...

  5. 经过4次优化我把python代码耗时减少95%

    背景交代 团队做大学英语四六级考试相关服务.业务中有一个care服务,购买了care服务考试不过可以全额退款,不过有一个前提是要完成care服务的任务,比如坚持背单词N天,完成指定的试卷. 在这个背景 ...

  6. 如何保证redis中存放的都是热点数据

    当redis使用的内存超过了设置的最大内存时,会触发redis的key淘汰机制,在redis 3.0中有6种淘汰策略: noeviction: 不删除策略.当达到最大内存限制时, 如果需要使用更多内存 ...

  7. Python 深拷贝和浅拷贝的区别

    python的复制,深拷贝和浅拷贝的区别    在python中,对象赋值实际上是对象的引用.当创建一个对象,然后把它赋给另一个变量的时候,python并没有拷贝这个对象,而只是拷贝了这个对象的引用  ...

  8. 使用json.net实现复杂对象转换为QueryString

    目标:生成复杂对象的QueryString,比如 new { Field1 = 1, Field2 = new { Field3 = "2", Field4 = new[] { n ...

  9. [hdu6978]New Equipments II

    显然可以费用流来做,具体建图如下-- 点集:源点,汇点,左边$n$​个工人,右边$n$​​​个设备 边集:源点向第$i$​个工人连$(1,a_{i})$​的边,第$i$​个设备向汇点连$(1,b_{i ...

  10. [gym102770L]List of Products

    有一个很重要的性质:若$a\le b$且$c\le d$,则$ac\le bd$ 根据这一性质,就可以利用单调性$o(n)$求出小于$a_{x}\cdot b_{y}$的数的个数(先要对$a$和$b$ ...