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. Zend Studio 离线汉化包下载方法

    进入eclipse官网 语言包位置 http://www.eclipse.org/babel/downloads.php

  2. Vue+Bootstrap实现购物车程序(3)

    效果展示:(说明:使用webpack重构购物车程序,使用vue-cli生成项目脚手架) 文件结构: 代码: (1)将原来编写的btn-grp组件单独编写到BtnGrp.vue文件中 可以看到现在代码清 ...

  3. asp.net C# 获得配置文件AppSettings 的值

    using System.Configuration;//导入命名空间 //配置文件 Web.config <appSettings> <!--数据连接字符串--> <a ...

  4. 简单的linux看门狗脚本

    watchdog.sh #!/bin/bash now=`date '+%Y-%m-%d %H:%M:%S'` baseDir=$(cd `dirname $0`; pwd) sleepTime=10 ...

  5. uLua-案例学习-API

    LuaState.Start()初始化吧 LuaState.AddSearchPath(string fullPath)增加搜索路径,这样在执行lua文件时就不需要输入全路径,类似环境变量path. ...

  6. Linux中搭建FTP服务器

    FTP工作原理 (1)FTP使用端口 [root@localhost ~]# cat /etc/services | grep ftp ftp-data 20/tcp #数据链路:端口20 ftp 2 ...

  7. C#上位机开发(一)—— 了解上位机

    在单片机项目开发中,上位机也是一个很重要的部分,主要用于数据显示(波形.温度等).用户控制(LED,继电器等),下位机(单片机)与 上位机之间要进行数据通信的两种方式都是基于串口的: USB转串口 — ...

  8. Unity 3D 使用TerrainCompose 调用RTP 报错:

    Unity 3D:5.2 version TerrainCompose:1.92 version RTP:3.2d version Unity 3D  使用TerrainCompose 调用RTP 报 ...

  9. STM32F407 跑马灯 寄存器版 个人笔记

    更多原理请参考跑马灯 库函数版 个人笔记 步骤 使能IO口时钟.配置相关寄存器寄存器RCC->AHB1ENR 初始化IO口模式.配置四个配置寄存器 GPIOx_MODER/ GPIOx_OTYP ...

  10. CodeForcesGym 100212E Long Dominoes

    Long Dominoes Time Limit: 1000ms Memory Limit: 65536KB This problem will be judged on CodeForcesGym. ...