RTTI(RunTime Type Information)执行时类型信息 具体解释

本文地址: http://blog.csdn.net/caroline_wendy/article/details/24369987

RTTI, RunTime Type Information, 执行时类型信息, 是多态的主要组成部分,

通过执行时(runtime)确定使用的类型, 执行不同的函数,复用(reuse)接口.

dynamic_cast<>能够 使基类指针转换为派生类的指针, 通过推断指针的类型, 能够决定使用的函数.

typeid(), 能够推断类型信息, 推断指针指向位置, 在多态中, 能够推断基类还是派生类.

代码:

/*
* test.cpp
*
* Created on: 2014.04.22
* Author: Spike
*/ /*eclipse cdt, gcc 4.8.1*/ #include <iostream>
#include <typeinfo> using namespace std; class Base {
public:
virtual void fcnA() {
std::cout << "base" << std::endl;
}
}; class Derived : public Base {
public:
virtual void fcnB() {
std::cout << "derived" << std::endl;
}
}; void fcnC(Base* p) {
Derived* dp = dynamic_cast<Derived*>(p);
if (dp != NULL)
dp->fcnB();
else
p->fcnA();
} void fcnD(Base* p) {
if (typeid(*p) == typeid(Derived)) {
Derived* dp = dynamic_cast<Derived*>(p);
dp->fcnB();
} else
p->fcnA();
} int main(void) {
Base* cp = new Derived;
std::cout << typeid(cp).name() << std::endl;
std::cout << typeid(*cp).name() << std::endl;
std::cout << typeid(&(*cp)).name() << std::endl;
fcnC(cp);
fcnD(cp);
Base* dp = new Base;
fcnC(dp);
fcnD(dp); return 0;
}

输出:

P4Base
7Derived
P4Base
derived
derived
base
base

以上代码仅仅是演示样例,

详细使用时, 避免使用dynamic_cast<>和typeid()去推断类型, 直接通过多态就可以.

注意多态的绑定仅仅能通过指针, 如fcnC(Base*), 或引用, 如fcnD(Base&), 实现动态绑定,
两者效果同样;

都会依据输入的类型,动态绑定虚函数(virtual function).

代码例如以下:

/*
* test.cpp
*
* Created on: 2014.04.22
* Author: Spike
*/ /*eclipse cdt, gcc 4.8.1*/ #include <iostream>
#include <typeinfo> using namespace std; class Base {
public:
virtual void fcn() {
std::cout << "base" << std::endl;
}
}; class Derived : public Base {
public:
virtual void fcn() override {
std::cout << "derived" << std::endl;
}
}; void fcnC(Base* p) {
p->fcn();
} void fcnD(Base& p) {
p.fcn();
} int main(void) {
Base* cp = new Derived;
std::cout << typeid(cp).name() << std::endl;
std::cout << typeid(*cp).name() << std::endl;
fcnC(cp);
fcnD(*cp);
Base* dp = new Base;
fcnC(dp);
fcnD(*dp); Base& cr = *cp;
std::cout << typeid(&cr).name() << std::endl;
std::cout << typeid(cr).name() << std::endl;
fcnC(&cr);
fcnD(cr);
Base& dr = *dp;
fcnC(&dr);
fcnD(dr); return 0;
}

输出:

P4Base
7Derived
derived
derived
base
base
P4Base
7Derived
derived
derived
base
base

C++ - RTTI(RunTime Type Information)执行时类型信息 具体解释的更多相关文章

  1. 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 ...

  2. c++对象模型和RTTI(runtime type information)

    在前面已经探讨过了虚继承对类的大小的影响,这次来加上虚函数和虚继承对类的大小的影响. 先来回顾一下之前例子的代码: #include <iostream> using namespace ...

  3. RTTI(Runtime Type Information )

    RTTI 是“Runtime Type Information”的缩写,意思是:运行时类型信息.它提供了运行时确定对象类型的方法.本文将简略介绍 RTTI 的一些背景知识.描述 RTTI 的概念,并通 ...

  4. c++ RTTI(runtime type info)

    RTTI(Run-Time Type Information,通过运行时类型信息)程序能够使用基类的指针或引用来检查这些指针或引用所指的对象的实际派生类型. RTTI提供了以下两个非常有用的操作符: ...

  5. Dynamic type checking and runtime type information

    动态类型的关键是将动态对象与实际类型信息绑定. See also: Dynamic programming language and Interpreted language Dynamic type ...

  6. 【JavaSE】运行时类型信息(RTTI、反射)

    运行时类型信息使得你可以在程序运行时发现和使用类型信息.--<Think in java 4th> **** 通常我们在面向对象的程序设计中我们经常使用多态特性使得大部分代码尽可能地少了解 ...

  7. 是否含有RTTI(运行时类型信息)是动态语言与静态语言的主要区别

    运行时类型信息代表类型信息和对内存的操作能力. 运行时类型信息是运行时系统的基础. 类型信息分为编译时类型信息和运行时类型信息两种: 静态语言的类型信息只在编译时使用和保留,在可执行文件中没有类型信息 ...

  8. 了解运行时类型信息(RTTI)

    RTTI需要引用单元TypeInfo GetPropInfo 函数用于获得属性的 RTTI 指针 PPropInfo.它有四种重载形式,后面三种重载的实现都是调用第一种形式.AKinds 参数用于限制 ...

  9. RTTI (Run-Time Type Identification,通过运行时类型识别) 转

    参考一: RTTI(Run-Time Type Identification,通过运行时类型识别)程序能够使用基类的指针或引用来检查这些指针或引用所指的对象的实际派生类型.   RTTI提供了以下两个 ...

随机推荐

  1. Flask框架 之数据库扩展Flask-SQLAlchemy

    一.安装扩展 pip install flask-sqlalchemy pip install flask-mysqldb 二.SQLAlchemy 常用的SQLAlchemy字段类型 类型名 pyt ...

  2. 第2节 mapreduce深入学习:14、mapreduce数据压缩-使用snappy进行压缩

    第2节 mapreduce深入学习:14.mapreduce数据压缩-使用snappy进行压缩 文件压缩有两大好处,节约磁盘空间,加速数据在网络和磁盘上的传输. 方式一:在代码中进行设置压缩 代码: ...

  3. iOS网络图片缓存SDWebImage

    Web image(网络图像) 该库提供了一个支持来自Web的远程图像的UIImageView类别 它提供了: 添加网络图像和缓存管理到Cocoa Touch framework的UIImageVie ...

  4. C++链表STL

    #include <iostream> #include <list> #include <algorithm> #include <stdlib.h> ...

  5. CSU 2018年12月月赛 D 2216 : Words Transformation

    Description There are n words, you have to turn them into plural form. If a singular noun ends with ...

  6. [HNOI/AHOI2018]转盘(线段树优化单调)

    gugu  bz lei了lei了,事独流体毒瘤题 一句话题意:任选一个点开始,每个时刻向前走一步或者站着不动 问实现每一个点都在$T_i$之后被访问到的最短时间 Step 1 该题可证: 最优方案必 ...

  7. mysql崩溃恢复

    mysql进程崩溃. 杀掉所有mysql进程,在my.cnf文件中写入innodb_recover_force=1,强制并忽略任何错误启动数据库. 用mysqldump导出所有数据,在新机器上部署好m ...

  8. STL优先队列重载

    priority_queue默认是大根堆,如果需要使用小根堆,如下 int main(){ priority_queue<int,vector<int>,greater<int ...

  9. 获取某一个<tr>中<td>的值

    $("#trId").children("td").eq(0).text(};    //当前行的第一个<td>的值    <td>下标 ...

  10. Master of Subgraph

    Problem E. Master of SubgraphYou are given a tree with n nodes. The weight of the i-th node is wi. G ...