c++ RTTI(runtime type info)
RTTI(Run-Time Type Information,通过运行时类型信息)程序能够使用基类的指针或引用来检查这些指针或引用所指的对象的实际派生类型.
typeid函数
type_info类
classtype_info{private:type_info(consttype_info&);type_info&operator=(consttype_info&);//type_info类的复制构造函数和赋值运算符是私有的。public:virtual~type_info();//析构函数booloperator==(consttype_info&)const;//在type_info类中重载了==运算符,该运算符可以比较两个对象的类型是否相等。booloperator!=(consttype_info&)const;//重载的!=运算符,以比较两个对象的类型是否不相等constchar*name()const;//使用得较多的成员函数name,该函数反回对象的类型的名字。前面使用的typeid(a).name()就调用了该成员函数boolbefore(consttype_info&);};typeid函数怎样创建type_info类的对象
// expre_typeid_Operator.cpp
// compile with: /GR /EHsc
#include <iostream>
#include <typeinfo.h> class Base
{
public:
virtual void vvfunc() {}
}; class Derived : public Base {}; using namespace std; int main()
{
Derived* pd = new Derived;
Base* pb = pd;
int i = ; cout << typeid( i ).name() << endl; // prints "int"
cout << typeid( 3.14 ).name() << endl; // prints "double"
cout << typeid( pb ).name() << endl; // prints "class Base *"
cout << typeid( *pb ).name() << endl; // prints "class Derived"
cout << typeid( pd ).name() << endl; // prints "class Derived *"
cout << typeid( *pd ).name() << endl; // prints "class Derived" delete pd;
}
typeid最常见的用途是比较两个表达式的类型,或者将表达式的类型与特定类型相比较。
Base *bp;
Derived *dp;
// compare type at run time of two objects
if (typeid(*bp) == typeid(*dp))
{
// bp and dp point to objects of the same type
}
// test whether run time type is a specific type
if (typeid(*bp) == typeid(Derived))
{
// bp actually points a Derived
}
注意:如果是typeid(bp),则是对指针进行测试,这会返回指针(bp)的静态编译时类型(Base *)。
如果指针p的值是0,,并且指针所指的类型是带虚函数的类型,则typeid(*p)抛出一个bad_typeid异常。
参考:http://baike.baidu.com/view/1042388.htm
更多:
http://blog.csdn.net/acdnjjjdjkdckjj/article/details/6326189
c++ RTTI(runtime type info)的更多相关文章
- C++ - RTTI(RunTime Type Information)执行时类型信息 具体解释
RTTI(RunTime Type Information)执行时类型信息 具体解释 本文地址: http://blog.csdn.net/caroline_wendy/article/details ...
- 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 v ...
- c++对象模型和RTTI(runtime type information)
在前面已经探讨过了虚继承对类的大小的影响,这次来加上虚函数和虚继承对类的大小的影响. 先来回顾一下之前例子的代码: #include <iostream> using namespace ...
- RTTI (Run-Time Type Identification,通过运行时类型识别) 转
参考一: RTTI(Run-Time Type Identification,通过运行时类型识别)程序能够使用基类的指针或引用来检查这些指针或引用所指的对象的实际派生类型. RTTI提供了以下两个 ...
- RTTI(Runtime Type Information )
RTTI 是“Runtime Type Information”的缩写,意思是:运行时类型信息.它提供了运行时确定对象类型的方法.本文将简略介绍 RTTI 的一些背景知识.描述 RTTI 的概念,并通 ...
- Dynamic type checking and runtime type information
动态类型的关键是将动态对象与实际类型信息绑定. See also: Dynamic programming language and Interpreted language Dynamic type ...
- Run-time type information--RTTI
In computer programming, run-time type information or run-time type identification (RTTI)[1] refers ...
- Java RTTI机制与反射机制
1.1 什么是RTTI? 维基百科的定义:In computer programming, RTTI (Run-Time Type Information, or Run-Time Type Iden ...
- java之RTTI和反射的理解
最近在读 Thinking in Java 这本书.读到类型信息这一张时,刚开始对书中所说的RTTI和反射彻底混了,不知道有什么联系,有哪些相同点和区别.于是在网上又找了些内容辅助理解,这一章又重新读 ...
随机推荐
- Linux下搭建Oracle11g RAC(1)----IP分配与配置IP
首先需要说明的,我的RAC搭建不是在虚拟机上完成的,而是在实际部署中,二者之间有些许差异,本人水平有限,请见谅. 其中,每台机器至少需要配置3个IP地址,在安装操作系统的过程中,我们需要配置公网IP和 ...
- redis安装与使用
linux: 1.下载redis 下载redis: 可以在redis的官网下载 : http://redis.io/ 也可以去谷歌的code下载: http://code.google.com/p/r ...
- Library中的title与Name
在Library中新增Title字段,其中文件夹的title字段与Name相同,并且默认生成:但是文件的Title字段为空.
- AndroidStudio1.4 manifest 中注册Activity时的错误提示解决办法
问题截图如下: 解决办法截图如下: 1: File->setting->Editor->Language Injections到如下界面 2:双击右侧选中的Item进入编辑界面 3: ...
- C# 进程 和线程
进程 没有应用程序可以看做是一个进程 线程:就是对cpu执行的最小单位 单线程:前台线程和后台线程 带来的问题:假死 net中不能跨线程访问
- 1 Yoga3 系统装机总结.
1- Yoga 3 存在串口驱动不安装, 那么触摸屏不能用的情况, 打破了以往对触摸屏-"纯外设" 的设想, 与系统有关!!! 2- 系统安装总结: 1) BIOS中设置UEFI ...
- Android中解析XML的方法
假设我要解析如下的XML文件: <?xml version="1.0" encoding="UTF-8"?> <books> <b ...
- LVS高可用集群
高可用LVS 集群构建 在LVS集群当中Director的作用是很关键的,所以我们在生产环境中要保证其高可用. 高可用架构图: 1.通过 piranha搭建LVS高可用性集群 piranha是REDH ...
- Java学习----Math函数
public class TestMath { public static void main(String[] args) { System.out.println(Math.E); System. ...
- WebMethod 属性
WebMethod有以下几种属性: BufferResponse CacheDuration Description EnableSession MessageName TransactionOpti ...