RTTI (Run-time type information) in C++
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++的更多相关文章
- RTTI(Runtime Type Information )
RTTI 是“Runtime Type Information”的缩写,意思是:运行时类型信息.它提供了运行时确定对象类型的方法.本文将简略介绍 RTTI 的一些背景知识.描述 RTTI 的概念,并通 ...
- C++ - RTTI(RunTime Type Information)执行时类型信息 具体解释
RTTI(RunTime Type Information)执行时类型信息 具体解释 本文地址: http://blog.csdn.net/caroline_wendy/article/details ...
- 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 ...
- RTTI (Run-Time Type Identification,通过运行时类型识别) 转
参考一: RTTI(Run-Time Type Identification,通过运行时类型识别)程序能够使用基类的指针或引用来检查这些指针或引用所指的对象的实际派生类型. RTTI提供了以下两个 ...
- Dynamic type checking and runtime type information
动态类型的关键是将动态对象与实际类型信息绑定. See also: Dynamic programming language and Interpreted language Dynamic type ...
- TIJ——Chapter Fourteen:Type Information
Runtime type information(RTTI) allows you to discover and use type information while a program is ru ...
- (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 ...
- TIJ学习--RTTI(Runtime-Time Type Identification)
TIJ学习--RTTI(Runtime-Time Type Identification) RTTI 运行时类型检查机制 获取一个类的Class引用的三种方法 class TestClass{} Te ...
- c++对象模型和RTTI(runtime type information)
在前面已经探讨过了虚继承对类的大小的影响,这次来加上虚函数和虚继承对类的大小的影响. 先来回顾一下之前例子的代码: #include <iostream> using namespace ...
随机推荐
- AliRTC 开启视频互动 “零计算” 时代
在 2021 云栖大会<产业视频化创新与最佳实践>视频云主题论坛中,阿里云智能高级技术专家在<AliRTC 开启视频互动 "零处理" 时代>的主题演讲中,发 ...
- robotframework定位页面内Frame框架里的元素
在自动化开发中,会遇到在页面内部点开一个新的页面后,新的页面元素无法定位到的情况,如点击下图的上传图片,弹出的界面无法直接定位到: 遇到这种情况需要查看弹出界面是否是Frame框架页面:打开火狐浏览器 ...
- mybatis之结果集的映射方式
查询的几种情况 // 1)查询单行数据返回单个对象 public Employee getEmployeeById(Integer id ); // 2) 查询多行数据返回对象的集合 public L ...
- uni-app路径规划(打开第三方地图实现)
百度网盘链接:https://pan.baidu.com/s/1-Ys13GFcnKXB1wkJotcwMw 提取码:16gp 把js文件放在common目录下 引入: import pathP ...
- go输入Hello word
package main import "fmt" func main() { fmt.Println("hello word") } 输入hello ...
- linux查看和修改时间
查看时间: # date Fri Jan 11 18:04:10 CST 2020设置时间 # date -s "19:20:30"设置日期+时间 # date -s " ...
- AsExpandable EF多条件查询
我个人学习新技术有一个方法,如果遇到问题会根据以前的经验来寻找一些类似的解决方法.有人会说,如果这个问题在你的学习或者工作生涯中都没有遇到过呢?很简单,通过搜索资料或查阅相关书籍学习别人的经验. 在如 ...
- MySQL基础语句(MySQL内置函数 )
MySQL 字符串函数 函数 描述 实例 ASCII(s) 返回字符串 s 的第一个字符的 ASCII 码. 返回 CustomerName 字段第一个字母的 ASCII 码: SELECT ASCI ...
- 使用.NET5、Blazor和Electron.NET构建跨平台桌面应用
Electron.NET是一个嵌入了ASP.NET Core的Electron的封装,通过Electron.NET可以构建基于.NET5的跨平台的桌面应用,使得开发人员只需要使用ASP.NET Cor ...
- [loj3046]语言
定义$S_{i}$表示第$i$条链所包含的点的集合,$(x,y)$合法当且仅当$x\ne y$且$\exists i,\{x,y\}\subseteq S_{i}$(答案即$\frac{合法点对数}{ ...