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和反射彻底混了,不知道有什么联系,有哪些相同点和区别.于是在网上又找了些内容辅助理解,这一章又重新读 ...
随机推荐
- Oracle学习----集群因子(Clustering Factor)
1.集群因子的算法: 通过dbms_rowid.rowid_block_number(rowid)找到记录对应的block 号.索引中记录了rowid,因此oracle 就可以根据索引中的rowid来 ...
- GUI编程笔记(java)01:GUI和CLI
GUI Graphical User Interface(图形用户接口). 用图形的方式,来显示计算机操作的界面,这样更方便更直观. CLI Command line User Interface ( ...
- NoteExpress格式化复制指定输出样式
在NoteExpress中没有看到为命令“选中的题录右击 => 复制题录 => 格式化复制”指定输出样式的明确配置项,但格式化复制的输出样式也是可以变化了,随细节大面板里的“预览”标签页里 ...
- FreeBSD系统更新与软件安装方法
一.系统更新 freebsd-update fetch freebsd-update install 二.软件源更新(类似yum update.apt-get update) 1.取回源 portsn ...
- 使用API在DigitalOcean上创建VPS
1.生成Personal Access Token(API-Token) 密钥类似如下格式: 81d58e36224b63fc2gedac14342d0cfb16vf5451c798b2a38f976 ...
- Linux重复执行上一条命令
执行刚刚执行的一条命令: !! 执行最近一个以指定字符串开头的命令(比如man) !man !m 引用上一个命令的最后一个参数 !$ <ESC>, .
- CodeSmith中SchemaExplorer属性的介绍
CodeSmith与数据库的联系,在CodeSmith中自带一个程序集SchemaExplorer.dll,这个程序集中的类主要用于获取数据库中各种对象的结构. <%@ Property Nam ...
- html复选框多行排列布局
前言:写这篇文章,主要是在于总结一下自己的心得体会... 公司的产品需要实现操作权限配置,我们实现的方式是,左边是“产品”=>“模块”树,右边是由“菜单”和“按钮”复选框按钮.如下图:
- Linux如何查找大文件或目录总结-1127
原帖地址:http://www.cnblogs.com/kerrycode/p/4391859.html 谢谢潇湘隐者,谢谢老大 在Linux系统中,如何去搜索一些比较大的文件呢?下面我整理了一下在 ...
- POJ 2039 To and Fro(模拟)
To and Fro Description Mo and Larry have devised a way of encrypting messages. They first decide sec ...