typeid 运算符

语法

typeid(type-id)
typeid(expression)

备注

typeid 运算符允许在运行时确定对象的类型。

typeid 的结果是 const type_info&。 该值是对表示 type-id 或 expression 的类型的 type_info 对象的引用,具体取决于所使用的 typeid 的形式。 有关详细信息,请参阅 type_info 类

typeid 运算符不适用于托管类型(抽象声明符或实例)。 有关获取指定类型的 Type 的信息,请参阅 typeid

typeid 运算符在应用于多态类类型的 lvalue 时执行运行时检查,其中对象的实际类型不能由提供的静态信息确定。 此类情况是:

  • 对类的引用

  • 使用 * 取消引用的指针

  • 带下标的指针 ([ ])。 (将下标与指向多态类型的指针一起使用是不安全的。)

如果 expression 指向基类类型,但该对象实际上是派生自该基类的类型,则派生类的 type_info 引用是结果。 expression 必须指向多态类型(具有虚函数的类)。 否则,结果是 expression 中引用的静态类的 type_info。 此外,必须取消引用指针,这样使用的对象就是它所指向的对象。 如果不取消引用指针,结果将是指针的 type_info,而不是它指向的内容。 例如:

// expre_typeid_Operator.cpp
// compile with: /GR /EHsc
#include <iostream>
#include <typeinfo>

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;
}

如果 expression 正在取消引用某个指针,并且该指针的值是零,typeid 将引发 bad_typeid 异常。 如果指针不指向有效的对象,则会引发 __non_rtti_object 异常。 它指示试图分析触发了错误的 RTTI,因为该对象在某种程度上是无效的。 (例如,它是一个错误的指针,或者代码不是使用 /GR 编译的。)

如果 expression 既不是指针,也不是对对象的基类的引用,则结果是表示 expression 的静态类型的 type_info 引用。 表达式的 static type 将引用在编译时已知的表达式的类型。 在计算表达式的静态类型时,将忽略执行语义。 此外,在确定表达式的静态类型时,将忽略引用(如果可能):

// expre_typeid_Operator_2.cpp
#include <typeinfo>

int main()
{
   typeid(int) == typeid(int&); // evaluates to true
}

typeid 也可以在模板中使用,以确定模板参数的类型:

// expre_typeid_Operator_3.cpp
// compile with: /c
#include <typeinfo>
template < typename T >
T max( T arg1, T arg2 ) {
   cout << typeid( T ).name() << "s compared." << endl;
   return ( arg1 > arg2 ? arg1 : arg2 );
}

另请参阅

运行时类型信息
关键字