C++的RTTI包含三部分內容:dynamic_cast, typeid和type_info(很少直接使用,但是需要了解一下,type_info的定義是由編譯器定的,但是共同的是它包含一個name()函數)。
#include <iostream>
#include <typeinfo>
using namespace std;
class Base1
{
};
class Derive1 : public Base1
{
};
class Base2
{
virtual void fun( void ) {}
};
class Derive2 : public Base2
{
};
class Derive22 : public Base2
{
};
int main( void )
{
// sample 1
cout << typeid( 1.1f).name() << endl; // 輸出float
// sample 2
Derive1 d1;
Base1& b1 = d1;
cout << typeid(b1).name() << endl; // 輸出"class Base1",因為Derive1和Base1之間沒有多態性
// sample 3
Derive2 d2;
Base2& b2 = d2;
cout << typeid(b2).name() << endl; // 輸出"class Derive2",因為Derive1和Base1之間有了多態性
// sample 4
// 指針強制轉化失敗后可以比較指針是否為零,而引用卻沒辦法,所以引用制轉化失敗后拋出異常
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; // 輸出"true",因為b2本身不是Derive2
// 引用轉化
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中卻不行
{
cout << " false " << endl;
}
return 0;
}
#include <typeinfo>
using namespace std;
class Base1
{
};
class Derive1 : public Base1
{
};
class Base2
{
virtual void fun( void ) {}
};
class Derive2 : public Base2
{
};
class Derive22 : public Base2
{
};
int main( void )
{
// sample 1
cout << typeid( 1.1f).name() << endl; // 輸出float
// sample 2
Derive1 d1;
Base1& b1 = d1;
cout << typeid(b1).name() << endl; // 輸出"class Base1",因為Derive1和Base1之間沒有多態性
// sample 3
Derive2 d2;
Base2& b2 = d2;
cout << typeid(b2).name() << endl; // 輸出"class Derive2",因為Derive1和Base1之間有了多態性
// sample 4
// 指針強制轉化失敗后可以比較指針是否為零,而引用卻沒辦法,所以引用制轉化失敗后拋出異常
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; // 輸出"true",因為b2本身不是Derive2
// 引用轉化
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中卻不行
{
cout << " false " << endl;
}
return 0;
}
下面的程序演示typeid的使用:
#include <iostream>
#include <typeinfo>
using namespace std;
class A
{
virtual void func(){};
};
class B: public A
{
void func(){};
};
int main()
{
A *pa;
B b,*pb;
pb = &b;
pa = pb;
std::cout<< " Name1: "
<< (typeid(pa).name()) <<endl;
std::cout<< " Name2: "
<<(typeid(pb).name())
<<endl;
std::cout<<boolalpha<< " *pa == *pb: "<< (typeid(*pa) == typeid(*pb))<<endl;
return 0;
}
#include <typeinfo>
using namespace std;
class A
{
virtual void func(){};
};
class B: public A
{
void func(){};
};
int main()
{
A *pa;
B b,*pb;
pb = &b;
pa = pb;
std::cout<< " Name1: "
<< (typeid(pa).name()) <<endl;
std::cout<< " Name2: "
<<(typeid(pb).name())
<<endl;
std::cout<<boolalpha<< " *pa == *pb: "<< (typeid(*pa) == typeid(*pb))<<endl;
return 0;
}
輸出是:
Name1:class A *
Name2:class B *
*pa == *pb:true
typeid(pa)返回的是指針類型。如果要返回對象的類型需要使用引用或者對象作為參數傳遞給typeid。
dynamic_cast是個運行時操作,但是它完全依賴虛函數表,如果某個兩個類之間有繼承關系,但是沒有虛函數,那么dynamic_cast不能進行轉化,也不能從void *都某個具體類的轉化,會報告編譯時錯誤。
