C++中的type_info(中英文版)

来源:互联网 发布:手机电话号码数据库 编辑:程序博客网 时间:2024/06/07 07:07
注:上课讲的dynamic_cat与这儿的type_info都属于RTII的内容,type_info的内容如下。要注意的是,由于C++标准对于type定义的稍显粗略,故不同编译器可能会所不同。
(From:http://www.cplusplus.com/reference/typeinfo/type_info/)
class
<typeinfo>

std::type_info

class type_info;
Type information type

Stores information about a type.

This type is returned by the typeid operator (as a const-qualified lvalue). Although the dynamic type may be any class publicly derived from it.

It can be used to compare two types or to retrieve information identifying a type.

typeid can be applied to any type or typed expression.
If applied to a reference type (lvalue), the type_info returned identifies the referenced type. Anyconst orvolatile qualified type is identified as its unqualified type.

A typedef type is considered the same as its aliased type.

When typeid is applied to a reference or dereferenced pointer to an object of a polymorphic class type (a class declaring or inheriting a virtual function), it considers its dynamic type (i.e., the type of the most derived object). This requires the RTTI (Run-time type information) to be available.

When typeid is applied to a dereferenced null pointer a bad_typeid exception is thrown.

The lifetime of the object returned by typeid extends to the end of the program.


译文:

==============================

class
<typeinfo>

std::type_info

class type_info;
类型信息

存储一种类型的类型信息。(译者注:包括基本类型和自定义类型)

可通过操作符typeid返回这个类型(作为常量修饰左值)。动态类型可以是任何从它派生的类型。

它可用于比较两种类型或是获取标识一个类的类型信息。

typeid能用于任何类型或是类型表达式。
如果应用于一个引用类型(左值),返回的type_info 被标识为引用类型。任何const或是volate修饰符类型被别识为无修饰类型。

typedef类型被视作其原始类型(别名类型)。

当typeid应用于引用类型或是解引用类型指针指向的一个对象,对象的类型是一个多态类型(声明或是继承了一个虚函数的一个类,【译者:一定注意这一点】),typeid可以适别这个动类型(也就是或父类指针指向了一个子类,则返回的是子类唯一识别符)。要做到一点需要RTTI的支持(译者注:GCC编译时,默认打开RTTI,其它编译器需要看其说明文档)。

当typeid应用于一个空指针时,会抛出bad_typeid异常。
由typeid获取的对象的生命周期是直至程序结束。