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和反射彻底混了,不知道有什么联系,有哪些相同点和区别.于是在网上又找了些内容辅助理解,这一章又重新读 ...
随机推荐
- iOS UIKit:animation
@import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css); @import url(/ ...
- CentOS 6.7 安装配置BT下载工具Transmission
1.配置额外yum源 i386 cd /etc/yum.repos.d/ wget http://geekery.altervista.org/geekery-el6-i686.repo x86_64 ...
- Effective C++ 笔记二 构造/析构/赋值运算
条款05:了解C++默默编写并调用哪些函数 编译器默认声明一个default构造函数.一个copy构造函数.一个copy assignment操作符和一个析构函数.这些函数都是public且inlin ...
- 30、ADO.NET、事务、DataSet
ADO.NET ADO.NET是一组用于和数据源进行交互的面向对象类库.通常数据源是数据库,但也可以是文本文件.Excel表格.XML文件. 说白了就是使用.net操作数据库的一套类库. ADO.NE ...
- 我对WindowsPhone8的一些反大众看法.
最近看到网上好多人抱怨wp8的系统设计.我个人有一些观点.实在是不吐不快! 1.为什么系统没有重力感应开关. 答:因为这部分开关功能不是wp8所必备的.只是APP的一个功能.所以.错误在APP上因 ...
- java: Eclipse jsp tomcat 环境搭建(完整)
] 欢迎您! 要学习一门语言,首先要做的就是搭建环境,然后能写一个小的Demo(类似Helloworld),不仅可以建立信心,而且还可以为之后的学习搭建一个验证平台,事半功倍. net领域的vs,号称 ...
- oracle sql语句
一.ORACLE的启动和关闭1.在单机环境下要想启动或关闭ORACLE系统必须首先切换到ORACLE用户,如下su - oracle a.启动ORACLE系统oracle>svrmgrlSVRM ...
- ORACLE如何停止一个JOB
ORACLE如何停止一个JOB1 相关表.视图2 问题描述为同事解决一个因为网络连接情况不佳时,执行一个超长时间的SQL插入操作.既然网络状况不好,就选择了使用一次性使用JOB来完成该插入操作.在JO ...
- UIScrollView -2(UIScrollView 与 UIPageControl的使用): 分页查看图片
1.初始化UIScrollView 2.设置初始化出来的UIScrollView的contentSize: myscrollview.contentSize =CGSizeMake(CGRectGet ...
- ios专题 - 单例模式的实现
[原创]http://www.cnblogs.com/luoguoqiang1985 单例模式是什么? 一个类只有一个实例. ----------------------- 这样做有什么好处? 在我的 ...