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和反射彻底混了,不知道有什么联系,有哪些相同点和区别.于是在网上又找了些内容辅助理解,这一章又重新读 ...
随机推荐
- 输入一个字符串,去掉重复的字符,并按ASCII值排序-华为机试
import java.util.Scanner; //输入字符串,去掉重复的字符,并按ASSIC码值排序 public class quChong { public static void main ...
- 重新温习,不但可以让自己对js更加的深刻认识,又能加深记忆,至此每天一次复习记录在此。
Undefined类型,它只有一个值就是它本身,打个比方你在声明变量的时候又未对变量赋值也就是相关初始化操作,那么此刻该变量的值便是Undefined. var a//声明一个变量. console. ...
- VS2010打不开VS2012 .NET MVC 工程,及打开后部分模块加载不正确的解决办法
转自http://www.xuebuyuan.com/2042634.html 首先,如果sln打开不正确,用(notepad++)打开sln 比如 VS2010的前两行为: Microsoft Vi ...
- 检测SqlServer服务器IO是否瓶颈
通过性能监视器监视 Avg. Disk Queue Length 小于2 Avg. Disk sec/Read , Avg. Disk sec/Write 小于10ms 可以用数据收集器定时收集 ...
- 非常的奇葩,终于解决了硬盘从盘盘符消失的问题 http://bbs.gamersky.com/thread-1712710-1-1.html (出处: 游民星空论坛)
本人用电脑也十多年了,硬盘的问题也碰到过不少.但最近却碰到了一个很奇葩的问题.就是安装了一块全新的SSD硬盘当从盘,但在装上之后,在我的电脑中却不显示,没有盘符.不过打开系统磁盘管理却能显示硬盘信息. ...
- Object-c 创建对象
创建对象有两种方法: 1. NSString *str = [NSString string]; 2. NSString *myStr = [[NSString alloc]init]; 第一种创建方 ...
- Win32中GDI+应用(四)--- 位图的打开与显示
显示位图,你应该使用GDI+里面的Bitmap类或者Image类,这两个类都提供了方法从硬盘上的一个文件打开文件,创建相应的内存中的位图对象的工作.然后你可以使用Graphics类的DrawImage ...
- 对N个数组进行操作。先把这N个一维数组合并成一个2为数组;然后进行操作
using System;using System.Collections.Generic;using System.Linq;using System.Collections;using Syste ...
- /etc/fstab一些信息
[root@shine shine]# vim /etc/fstab ## /etc/fstab# Created by anaconda on Thu May 9 13:29:35 2013## A ...
- js如何获取一个月的天数 data javascript
js如何获取一个月的天数 function days(year,month){ var dayCount; now = new Date(year,month, 0); dayCount = now. ...