typeid的用法

来源:互联网 发布:ps合成照片软件 编辑:程序博客网 时间:2024/05/16 02:15
本文摘自MSDN,本人在此将它翻译一下
typeid( type-id )typeid( expression )

Collapse imageRemarks

The typeid operator allows the type of an object to be determined at run time.

The result of typeid is a const type_info&. The value is a reference to a type_info object that represents either the type-id or the type of the expression, depending on which form of typeid is used. See type_info Class for more information.

The typeid operator does not work with managed types (abstract declarators or instances), see typeid for information on getting the Type of a specified type.

The typeid operator does a run-time check when applied to an l-value of a polymorphic class type, where the true type of the object cannot be determined by the static information provided. Such cases are:

  • A reference to a class

  • A pointer, dereferenced with *

  • A subscripted pointer (i.e. [ ]). (Note that it is generally not safe to use a subscript with a pointer to a polymorphic type.)

If the expression points to a base class type, yet the object is actually of a type derived from that base class, a type_info reference for the derived class is the result. The expression must point to a polymorphic type (a class with virtual functions). Otherwise, the result is the type_info for the static class referred to in the expression. Further, the pointer must be dereferenced so that the object it points to is used. Without dereferencing the pointer, the result will be the type_info for the pointer, not what it points to. For example:

 
// expre_typeid_Operator.cpp// compile with: /GR /EHsc#include <iostream>#include <typeinfo.h>class Base {public:   virtual void vvfunc() {}};class Derived : public Base {};using namespace std;int main() {   Derived* pd = new Derived;   Base* pb = pd;   cout << typeid( pb ).name() << endl;   //prints "class Base *"   cout << typeid( *pb ).name() << endl;   //prints "class Derived"   cout << typeid( pd ).name() << endl;   //prints "class Derived *"   cout << typeid( *pd ).name() << endl;   //prints "class Derived"   delete pd;}
下面是我的翻译
typeid( type-id )typeid( expression )
评论:
typeid运算符使得我们可以在程序运行期间来确定一个对象的类型。
预算符typeid的结果是一个const type_info&,即一个type_info的常量引用。
该引用代表type-id或者expression的类型,这取决与我们使用typeid的哪一种形式。
要获取更多信息请参考type_info类。
当用一个多态类类型作为左值时,typeid就会在运行时执行类型检查。
typeid通常用于对象的真正的类型无法通过所提供的静态信息确定的情况。
例如:
  • 类的引用
  • 一个指针,用*提领
  • 下标指针(如【】)
如果expression指向一个基类类型,然而对象确实是一个派生自基类的对象
那么typeid将返回一个派生类的type_info引用。但是expression必须指向一个
多态类,否则返回的将是静态类信息。此外,指针必须被提领,以便使用它所指向的对象,没有提领指针
,结果将是指针的type_info(这是一个静态信息),而不是它所指向的对象的type_info
如 :
class Derived : public Base {};using namespace std;int main() {   Derived* pd = new Derived;   Base* pb = pd;   cout << typeid( pb ).name() << endl;   //prints "class Base *"   cout << typeid( *pb ).name() << endl;   //提领,prints "class Derived"   cout << typeid( pd ).name() << endl;   //prints "class Derived *"   cout << typeid( *pd ).name() << endl;   //提领 prints "class Derived"   delete pd;}