C++ typeid关键字详解

来源:互联网 发布:网页在线客服源码 编辑:程序博客网 时间:2024/06/08 16:38

http://blog.csdn.net/gatieme

typeid关键字


注意:typeid是操作符,不是函数。这点与sizeof类似)

运行时获知变量类型名称,可以使用 typeid(变量).name()

需要注意不是所有编译器都输出”int”、”float”等之类的名称,对于这类的编译器可以这样使用

int ia = 3;if(typeid(ia) == typeid(int)){    cout <<"int" <<endl;}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 1
  • 2
  • 3
  • 4
  • 5

RTTI(Run-Time Type Identification)-运行时类型识别


在揭开typeid神秘面纱之前,我们先来了解一下RTTI(Run-Time Type Identification,运行时类型识别),它使程序能够获取由基指针或引用所指向的对象的实际派生类型,即允许“用指向基类的指针或引用来操作对象”的程序能够获取到“这些指针或引用所指对象”的实际派生类型。

在C++中,为了支持RTTI提供了两个操作符:dynamic_casttypeid

  • dynamic_cast允许运行时刻进行类型转换,从而使程序能够在一个类层次结构中安全地转化类型,与之相对应的还有一个非安全的转换操作符static_cast,因为这不是本文的讨论重点,所以这里不再详述,感兴趣的可以自行查阅资料。

  • typeid是C++的关键字之一,等同于sizeof这类的操作符。typeid操作符的返回结果是名为type_info的标准库类型的对象的引用(在头文件typeinfo中定义,稍后我们看一下vs和gcc库里面的源码),它的表达式有下图两种形式。

实现机制与使用技巧


type_info类对象类别判别


对象类别判别分析


如果表达式的类型是类类型且至少包含有一个虚函数,则typeid操作符返回表达式的动态类型,需要在运行时计算; 
否则,typeid操作符返回表达式的静态类型,在编译时就可以计算。

ISO C++标准并没有确切定义type_info,它的确切定义编译器相关的,但是标准却规定了其实现必需提供如下四种操作(在之后的章节中我会来分析type_info类文件的源码)

运算描述t1 == t2如果两个对象t1和t2类型相同,则返回true;否则返回falset1 != t2如果两个对象t1和t2类型不同,则返回true;否则返回falset.name()返回类型的C-style字符串,类型名字用系统相关的方法产生1t1.before(t2)返回指出t1是否出现在t2之前的bool值

type_info类提供了public虚 析构函数,以使用户能够用其作为基类。它的默认构造函数和拷贝构造函数及赋值操作符都定义为private,所以不能定义或复制type_info类型的对象。程序中创建type_info对象的唯一方法是使用typeid操作符(由此可见,如果把typeid看作函数的话,其应该是type_info的 友元)。type_info的name成员函数返回C-style的字符串,用来表示相应的类型名,但务必注意这个返回的类型名与程序中使用的相应类型名并不一定一致(往往如此,见后面的程序),这具体由编译器的实现所决定的,标准只要求实现为每个类型返回唯一的字符串。

type_info类源代码


使用sudo find / -name typeinfo.h来查找源码 
这里写图片描述

#ifndef _TYPEINFO#define _TYPEINFO#include <exception>namespace std {  class type_info   {  public:    virtual ~type_info();    { return __name[0] == '*' ? __name + 1 : __name; }    bool before(const type_info& __arg) const    { return __name < __arg.__name; }    bool operator==(const type_info& __arg) const    { return __name == __arg.__name; }    bool operator!=(const type_info& __arg) const    { return !operator==(__arg); }    virtual bool __is_pointer_p() const;    virtual bool __is_function_p() const;  protected:    const char *__name;    explicit type_info(const char *__n): __name(__n) { }  private:    type_info& operator=(const type_info&);    type_info(const type_info&);  };} // extern "C++"#endif
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42

示例1-基本数据类型


下表列出了使用typeid操作符的表达式的值

int a;double b;char * c;long d;
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4
运算描述typeid(a) == typeid(int)truetypeid(a) == typeid(float)falsetypeid(a) == typeid(int *)falsetypeid(b) == typeid(double)truetypeid(b) == typeid(float)falsetypeid(b) == typeid(long double)falsetypeid(c) == typeid(char *)truetypeid(c) == typeid(char)falsetypeid(c) == typeid(string)falsetypeid(d) == typeid(long)truetypeid(d) == typeid(int)false

操作符typeid返回的是一个type_info类(用于描述数据类型的一个系统类)对象的引用。这个操作符可以用于表达式和类型名(包括自定的数据类型,比如类)。

示例2-类对象


class base{public :    void m(){cout<<"base"<<endl;}};class derived : public base{public:    void m(){cout<<"derived"<<endl;}};
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

假设我们根据例2中定义的两个类来定义如下指针:

base * p = new derived;
  • 1
  • 1

下表将给出使用typeid操作符的结果。

运算描述typeid(p) == typeid(base*)truetypeid(p) == typeid(derived*)falsetypeid(*p) == typeid(base)truetypeid(*p) == typeid(derived)false

对于表达式typeid(p),同样,因为p是base*类型的指针,因此typeid(p) == typeid(base*)为真,而typeid(p) == typeid(derived*)为假。而对于表达式typeid(*p),由于此时的基类不具有多态性,因而*p将会采用编译期类型来计算,编译期*p是base对象,因此表达式typeid(*p) == typeid(derived)为假,typeid(*p) == typeid(base)为真。

示例3-带虚函数的基类


class base{public :    virtual void m(){cout<<"base"<<endl;}};class derived : public base{public:    void m(){cout<<"derived"<<endl;}};
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

假设我们如本例所示定义了两个类base类和derived类,基于这两个类定义,我们定义指针如下:

base * p = new derived;
  • 1
  • 1

下表将给出使用typeid操作符的结果。

运算描述typeid(p) == typeid(base*)truetypeid(p) == typeid(derived*)falsetypeid(*p) == typeid(base)falsetypeid(*p) == typeid(derived)true

对于表达式typeid(p),因为p是base*类型的指针,因此typeid(p) == typeid(base*)为真,而typeid(p) == typeid(derived*)为假。而对于表达式typeid(*p),因为base类具有多态性,因而在计算typeid(*p)时会根据运行时p所指向的实际类型去计算,而本例中p指向的是派生类对象,因此表达式typeid(*p) == typeid(derived)为真,typeid(*p) == typeid(base)为假。

异常处理bad_typeid


  class bad_typeid : public exception   {  public:    bad_typeid () throw() { }    // This declaration is not useless:    // http://gcc.gnu.org/onlinedocs/gcc-3.0.2/gcc_6.html#SEC118    virtual ~bad_typeid() throw();    // See comment in eh_exception.cc.    virtual const char* what() const throw();  };} // namespace std
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

这里写图片描述

参照

【C++】typeinfo.h

C++中的typeid关键字

0 0
原创粉丝点击