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. C++ IO基础

    一:c++I/O处理,按照数据输入输出的过程,形象的将其看做流.数据在流中进行传播. 所有的流有两个基类:ios和streambuf类 streambuf:提供对缓冲区的基本操作,设置缓冲区等 ios ...

  2. 官宣 .NET RC 2

    我们很高兴发布 .NET 6 RC(Release Candidate) 2.它是生产环境中支持的两个"go live"候选版本中的第二个. 在过去的几个月里,团队一直专注于质量的 ...

  3. 你以为我在玩游戏?其实我在学 Java

    大家好,我是程序员cxuan!今天和大家一起Look一下这个有趣的国外编程网站! 寓教于乐 "今天我们来学习 Java " . "Java 是一门面向对象的编程语言&qu ...

  4. Java学习(九)

    今天先学习了内联框架的知识,使用iframe的标签,还有超链接的知识. 做了个小实践 <!DOCTYPE html> <head> <meta charset=" ...

  5. Go语言核心36讲(Go语言实战与应用五)--学习笔记

    27 | 条件变量sync.Cond (上) 前导内容:条件变量与互斥锁 我们常常会把条件变量这个同步工具拿来与互斥锁一起讨论.实际上,条件变量是基于互斥锁的,它必须有互斥锁的支撑才能发挥作用. 条件 ...

  6. Python 爬取 煎蛋

    这个我是拿来参考的 import requests def url_open(url): response = requests.get(url) html = response.content re ...

  7. 数字逻辑实践2->Verilog编写规范

    来源:数字逻辑与Verilog设计实验课讲解,个人做的笔记与整理. 00 规范的重要性 良好的编程风格有利于减少消耗的硬件资源,提高设计的工作频率 . 提高系统的可移植性和可维护性. 程序的格式化能体 ...

  8. es使用java的api操作

    基本环境的创建 pom依赖  <?xml version="1.0" encoding="UTF-8"?> <project xmlns=&q ...

  9. Codeforces 1067D - Computer Game(矩阵快速幂+斜率优化)

    Codeforces 题面传送门 & 洛谷题面传送门 好题. 首先显然我们如果在某一次游戏中升级,那么在接下来的游戏中我们一定会一直打 \(b_jp_j\) 最大的游戏 \(j\),因为这样得 ...

  10. CF1542E2 Abnormal Permutation Pairs (hard version)

    CF1542E2 Abnormal Permutation Pairs (hard version) good tea. 对于两个排列 \(p,q\),如果 \(p\) 的字典序小于 \(q\),则要 ...