typeid是C++的关键字之一,等同于sizeof这类的操作符。typeid操作符的返回结果是名为type_info的标准库类型的对象的引用。
如果表达式的类型是类类型且至少包含有一个虚函数,则typeid操作符返回表达式的动态类型,需要在运行时计算;否则,typeid操作符返回表达式的静态类型,在编译时就可以计算。
测试1
#define OUTPUT(f) cout << #f << "\t: " << typeid(f).name() << endl;
class BaseA {};
class DeriveA: public BaseA {};
class BaseB
{
virtual void f(){}
};
class DeriveB: public BaseB {};
int main()
{
cout << "-------直接处理类名-------" <<endl;
OUTPUT(BaseA);
OUTPUT(DeriveA);
OUTPUT(BaseB);
OUTPUT(DeriveB);
cout << endl << "-------基类不含虚函数-------" <<endl;
BaseA baseA;
DeriveA deriveA;
OUTPUT(baseA);
OUTPUT(deriveA);
BaseA* pa;
pa = &baseA;
OUTPUT(*pa);
OUTPUT(pa);
pa = &deriveA;
OUTPUT(*pa);
OUTPUT(pa);
cout << endl << "-------基类含有虚函数-------" <<endl;
BaseB baseB;
DeriveB deriveB;
OUTPUT(baseB);
OUTPUT(deriveB);
BaseB* pb;
pb = &baseB;
OUTPUT(*pb);
OUTPUT(pb);
pb = &deriveB;
OUTPUT(*pb);
OUTPUT(pb);
}
1.当typeid操作符的操作数是不带有虚函数的类类型时,typeid操作符会指出操作数的类型,而不是底层对象的类型。
2.如果typeid操作符的操作数是至少包含一个虚拟函数的类类型时,并且该表达式是一个基类的引用,则typeid操作符指出底层对象的派生类类型。
测试2
运行时获知变量类型名称,可以使用 typeid(变量).name(),需要注意不是所有编译器都输出"int"、"float"等之类的名称,对于这类的编译器可以这样使用:float f = 1.1f; if( typeid(f) == typeid(0.0f) )。
补充:对非引用类型,typeid是在编译时期识别的,只有引用类型才会在运行时识别。
#include <iostream>
#include <typeinfo>
using namespace std;
int main(void)
{
// sample 1
cout << typeid(1.1f).name() << endl;
// sample 2
class Base1 {};
class Derive1 :public Base1 {};
Derive1 d1;
Base1& b1 = d1;
cout << typeid(b1).name() << endl; // 输出"class Base1",因为Derive1和Base1之间没有多态性
// sample 3, 编译时需要加参数 /GR
class Base2 {
virtual void fun(void) {}
};
class Derive2 :public Base2 { };
Derive2 d2;
Base2& b2 = d2;
cout << typeid(b2).name() << endl; // 输出"class Derive2",因为Derive1和Base1之间有了多态性
// sample 4
class Derive22 :public Base2 { }; // 指针强制转化失败后可以比较指针是否为零,而引用却没办法,所以引用制转化失败后抛出异常
Derive2* pb1 = dynamic_cast<Derive2*>(&b2);
cout << boolalpha << (0 != pb1) << endl; // 输出"true",因为b2本身确实是指向Derive2
Derive22* pb2 = dynamic_cast<Derive22*>(&b2);
cout << boolalpha << (0 != pb2) << endl; // 输出"false",因为b2本身不是指向Derive22
try {
Derive2& rb1 = dynamic_cast<Derive2&>(b2);
cout << "true" << endl;
}
catch (bad_cast) {
cout << "false" << endl;
}
try { Derive22& rb2 = dynamic_cast<Derive22&>(b2); cout << "true" << endl; }
catch (...) // 应该是 bad_cast,但不知道为什么在VC++6.0中却不行?因为VC++6.0默认状态是禁用 RTTI 的,启用方式:project->setting->c/c++->category->c++ Language 下面第二个复选框选中。
{
cout << "false" << endl;
}
system("pause");
return 0;
}
本文转自:
https://blog.csdn.net/zkybeck_ck/article/details/51762541