• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号
您的位置:首页 > 程序设计 >C语言 > C++中的RTTI机制详解

C++中的RTTI机制详解

作者:果冻想 字体:[增加 减小] 来源:互联网 时间:2017-05-28

果冻想 通过本文主要向大家介绍了c++ rtti,rtti,rtti是什么,java rtti,delphi rtti等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

前言

RTTI是”Runtime Type Information”的缩写,意思是运行时类型信息,它提供了运行时确定对象类型的方法。RTTI并不是什么新的东西,很早就有了这个技术,但是,在实际应用中使用的比较少而已。而我这里就是对RTTI进行总结,今天我没有用到,并不代表这个东西没用。学无止境,先从typeid函数开始讲起。

typeid函数

typeid的主要作用就是让用户知道当前的变量是什么类型的,比如以下代码:
#include <iostream>
#include <typeinfo>
using namespace std;
 
int main()
{
     short s = 2;
     unsigned ui = 10;
     int i = 10;
     char ch = 'a';
     wchar_t wch = L'b';
     float f = 1.0f;
     double d = 2;
 
     cout<<typeid(s).name()<<endl; // short
     cout<<typeid(ui).name()<<endl; // unsigned int
     cout<<typeid(i).name()<<endl; // int
     cout<<typeid(ch).name()<<endl; // char
     cout<<typeid(wch).name()<<endl; // wchar_t
     cout<<typeid(f).name()<<endl; // float
     cout<<typeid(d).name()<<endl; // double
 
     return 0;
}
</div>
对于C++支持的内建类型,typeid能完全支持,我们通过调用typeid函数,我们就能知道变量的信息。对于我们自定义的结构体,类呢?

#include <iostream>
#include <typeinfo>
using namespace std;
 
class A
{
public:
     void Print() { cout<<"This is class A."<<endl; }
};
 
class B : public A
{
public:
     void Print() { cout<<"This is class B."<<endl; }
};
 
struct C
{
     void Print() { cout<<"This is struct C."<<endl; }
};
 
int main()
{
     A *pA1 = new A();
     A a2;
 
     cout<<typeid(pA1).name()<<endl; // class A *
     cout<<typeid(a2).name()<<endl; // class A
 
     B *pB1 = new B();
     cout<<typeid(pB1).name()<<endl; // class B *
 
     C *pC1 = new C();
     C c2;
 
     cout<<typeid(pC1).name()<<endl; // struct C *
     cout<<typeid(c2).name()<<endl; // struct C
 
     return 0;
}
</div>

是的,对于我们自定义的结构体和类,tpyeid都能支持。在上面的代码中,在调用完typeid之后,都会接着调用name()函数,可以看出typeid函数返回的是一个结构体或者类,然后,再调用这个返回的结构体或类的name成员函数;其实,typeid是一个返回类型为type_info类型的函数。那么,我们就有必要对这个type_info类进行总结一下,毕竟它实际上存放着类型信息。

type_info类

去掉那些该死的宏,在Visual Studio 2012中查看type_info类的定义如下:
class type_info
{
public:
    virtual ~type_info();
    bool operator==(const type_info& _Rhs) const; // 用于比较两个对象的类型是否相等
    bool operator!=(const type_info& _Rhs) const; // 用于比较两个对象的类型是否不相等
    bool before(const type_info& _Rhs) const;
 
    // 返回对象的类型名字,这个函数用的很多
    const char* name(__type_info_node* __ptype_info_node = &__type_info_root_node) const;
    const char* raw_name() const;
private:
    void *_M_data;
    char _M_d_name[1];
    type_info(const type_info& _Rhs);
    type_info& operator=(const type_info& _Rhs);
    static const char * _Name_base(const type_info *,__type_info_node* __ptype_info_node);
    static void _Type_info_dtor(type_info *);
};
</div>

在type_info类中,复制构造函数和赋值运算符都是私有的,同时也没有默认的构造函数;所以,我们没有办法创建type_info类的变量,例如type_info A;这样是错误的。那么typeid函数是如何返回一个type_info类的对象的引用的呢?我在这里不进行讨论,思路就是类的友元函数。

typeid函数的使用

typeid使用起来是非常简单的,常用的方式有以下两种:

1.使用type_info类中的name()函数返回对象的类型名称

就像上面的代码中使用的那样;但是,这里有一点需要注意,比如有以下代码:
#include <iostream>
#include <typeinfo>
using namespace std;
 
class A
{
public:
     void Print() { cout<<"This is class A."<<endl; }
};
 
class B : public A
{
public:
     void Print() { cout<<"This is class B."<<endl; }
};
 
int main()
{
     A *pA = new B();
     cout<<typeid(pA).name()<<endl; // class A *
     cout<<typeid(*pA).name()<<endl; // class A
     return 0;
}
</div>

我使用了两次typeid,但是两次的参数是不一样的;输出结果也是不一样的;当我指定为pA时,由于pA是一个A类型的指针,所以输出就为class A *;当我指定*pA时,它表示的是pA所指向的对象的类型,所以输出的是class A;所以需要区分typeid(*pA)和typeid(pA)的区别,它们两个不是同一个东西;但是,这里又有问题了,明明pA实际指向的是B,为什么得到的却是class A呢?我们在看下一段代码:
#include <iostream>
#include <typeinfo>
using namespace std;
 
class A
{
public:
     virtual void Print() { cout<<"This is class A."<<endl; }
};
 
class B : public A
{
public:
     void Print() { cout<<"This is class B."<<endl; }
};
 
int main()
{
     A *pA = new B();
     cout<<typeid(pA).name()<<endl; // class A *
 &nb

分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

  • C++中的RTTI机制详解

相关文章

  • 2017-05-28解析linux 文件和目录操作的相关函数
  • 2017-05-28C语言变量类型与输出控制用法实例教程
  • 2017-05-28浅析c#中WebBrowser控件的使用方法
  • 2017-05-28C语言一维数组初步学习笔记
  • 2017-05-28一波C语言字符数组实用技巧集锦
  • 2017-05-28C语言冒泡排序算实现代码
  • 2017-05-28C语言中的内存泄露 怎样避免与检测
  • 2017-05-28基于atoi()与itoa()函数的内部实现方法详解
  • 2017-05-28MFC控件大小随窗体大小而改变
  • 2017-05-28C语言 while语句的用法详解

文章分类

  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号

最近更新的内容

    • C语言 typedef:给类型起一个别名
    • OnSize、OnSizing和OnGetMinMaxInfo区别分析
    • 浅谈C语言转义字符和格式控制符
    • 深入理解线程安全与Singleton
    • C++中的常量定义小结
    • c++ 临时对象的来源
    • c++将引用或者是指针作为函数参数实现实参的运算
    • 先序遍历二叉树的递归实现与非递归实现深入解析
    • 字符串的组合算法问题的C语言实现攻略
    • C语言实现在windows服务中新建进程的方法

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有