RTTI是”Runtime Type Information”的縮寫,意思是運行時類型信息,它提供了運行時確定對象類型的方法。
typeid函數
對於c++的內置數據類型,typeid可以方便的輸出它們的數據類型。
#include <iostream>
#include <typeinfo>
using namespace std;
int main()
{
short s = 2;
unsigned ui = 10;
int i = 10;
char ch = 'a';
wchar_t wch = L'b';
float f = 1.0f;
double d = 2;
cout<<typeid(s).name()<<endl; // short
cout<<typeid(ui).name()<<endl; // unsigned int
cout<<typeid(i).name()<<endl; // int
cout<<typeid(ch).name()<<endl; // char
cout<<typeid(wch).name()<<endl; // wchar_t
cout<<typeid(f).name()<<endl; // float
cout<<typeid(d).name()<<endl; // double
return 0;
}
對於自己創建的類對象,依然可以輸出它們的數據類型
#include <iostream>
#include <typeinfo>
using namespace std;
class A
{
public:
void Print() { cout<<"This is class A."<<endl; }
};
class B : public A
{
public:
void Print() { cout<<"This is class B."<<endl; }
};
struct C
{
void Print() { cout<<"This is struct C."<<endl; }
};
int main()
{
A *pA1 = new A();
A a2;
cout<<typeid(pA1).name()<<endl; // class A *
cout<<typeid(a2).name()<<endl; // class A
B *pB1 = new B();
cout<<typeid(pB1).name()<<endl; // class B *
C *pC1 = new C();
C c2;
cout<<typeid(pC1).name()<<endl; // struct C *
cout<<typeid(c2).name()<<endl; // struct C
return 0;
}
RTTI 核心
#include <iostream>
#include <typeinfo>
using namespace std;
class A
{
public:
void Print() { cout<<"This is class A."<<endl; }
};
class B : public A
{
public:
void Print() { cout<<"This is class B."<<endl; }
};
int main()
{
A *pA = new B();
cout<<typeid(pA).name()<<endl; // class A *
cout<<typeid(*pA).name()<<endl; // class A
return 0;
}
分析:
- 我使用了兩次typeid,但是兩次的參數是不一樣的;輸出結果也是不一樣的;當我指定為pA時,由於pA是一個A類型的指針,所以輸出就為class A * ;
- 當我指定*pA時,它表示的是pA所指向的對象的類型,所以輸出的是class A;
- 所以需要區分typeid(*pA)和typeid(pA)的區別,它們兩個不是同一個東西
但是,這里又有問題了,明明pA實際指向的是B,為什么得到的卻是class A?
#include <iostream>
#include <typeinfo>
using namespace std;
class A
{
public:
virtual void Print() { cout<<"This is class A."<<endl; }
};
class B : public A
{
public:
void Print() { cout<<"This is class B."<<endl; }
};
int main()
{
A *pA = new B();
cout<<typeid(pA).name()<<endl; // class A *
cout<<typeid(*pA).name()<<endl; // class B
return 0;
}
划重點:
好了,我將Print函數變成了虛函數,輸出結果就不一樣了,這說明什么?
這就是RTTI在搗鬼了,當類中不存在虛函數時,typeid是編譯時期的事情,也就是靜態類型,就如上面的cout<<typeid(*pA).name()<<endl;輸出class A一樣;
當類中存在虛函數時,typeid是運行時期的事情,也就是動態類型,就如上面的cout<<typeid(*pA).name()<<endl;輸出class B一樣,關於這一點,我們在實際編程中,經常會出錯,一定要謹記。
(這個真的很重要 一定要多看看 一個類里面有virutal 和沒有virtual 對於編譯器來說,做的事完全不同的事情,所有一定要看清楚這個類有沒有virtual)
type_info類里面的比較運算符
使用type_info類中重載的==和!=比較兩個對象的類型是否相等
這個會經常用到,通常用於比較兩個帶有虛函數的類的對象是否相等,例如以下代碼:
#include <iostream>
#include <typeinfo>
using namespace std;
class A
{
public:
virtual void Print() { cout<<"This is class A."<<endl; }
};
class B : public A
{
public:
void Print() { cout<<"This is class B."<<endl; }
};
class C : public A
{
public:
void Print() { cout<<"This is class C."<<endl; }
};
void Handle(A *a)
{
if (typeid(*a) == typeid(A))
{
cout<<"I am a A truly."<<endl;
}
else if (typeid(*a) == typeid(B))
{
cout<<"I am a B truly."<<endl;
}
else if (typeid(*a) == typeid(C))
{
cout<<"I am a C truly."<<endl;
}
else
{
cout<<"I am alone."<<endl;
}
}
int main()
{
A *pA = new B();
Handle(pA);
delete pA;
pA = new C();
Handle(pA);
return 0;
}
這是一種用法,呆會我再總結如何使用dynamic_cast來實現同樣的功能。
dynamic_cast機制
使用dynamic_cast 機制來實現上述的代碼(dynamic_cast 是一種很常用的方法)
#include <iostream>
#include <typeinfo>
using namespace std;
class A
{
public:
virtual void Print() { cout<<"This is class A."<<endl; }
};
class B
{
public:
virtual void Print() { cout<<"This is class B."<<endl; }
};
class C : public A, public B
{
public:
void Print() { cout<<"This is class C."<<endl; }
};
int main()
{
A *pA = new C;
//C *pC = pA; // Wrong 編譯器會提示錯誤
C *pC = dynamic_cast<C *>(pA);
if (pC != NULL)
{
pC->Print();
}
delete pA;
}
在上面代碼中,如果我們直接將pA賦值給pC,這樣編譯器就會提示錯誤,而當我們加上了dynamic_cast之后,一切就ok了。那么dynamic_cast在后面干了什么呢?
dynamic_cast主要用於在多態的時候,它允許在運行時刻進行類型轉換,從而使程序能夠在一個類層次結構中安全地轉換類型,把基類指針(引用)轉換為派生類指針(引用)。
當類中存在虛函數時,編譯器就會在類的成員變量中添加一個指向虛函數表的vptr指針,每一個class所關聯的type_info object也經由virtual table被指出來,通常這個type_info object放在表格的第一個slot。當我們進行dynamic_cast時,編譯器會幫我們進行語法檢查。如果指針的靜態類型和目標類型相同,那么就什么事情都不做;否則,首先對指針進行調整,使得它指向vftable,並將其和調整之后的指針、調整的偏移量、靜態類型以及目標類型傳遞給內部函數。其中最后一個參數指明轉換的是指針還是引用。兩者唯一的區別是,如果轉換失敗,前者返回NULL,后者拋出bad_cast異常。對於在typeid函數的使用中所示例的程序,我使用dynamic_cast進行更改,代碼如下:
#include <iostream>
#include <typeinfo>
using namespace std;
class A
{
public:
virtual void Print() { cout<<"This is class A."<<endl; }
};
class B : public A
{
public:
void Print() { cout<<"This is class B."<<endl; }
};
class C : public A
{
public:
void Print() { cout<<"This is class C."<<endl; }
};
void Handle(A *a)
{
if (dynamic_cast<B*>(a))
{
cout<<"I am a B truly."<<endl;
}
else if (dynamic_cast<C*>(a))
{
cout<<"I am a C truly."<<endl;
}
else
{
cout<<"I am alone."<<endl;
}
}
int main()
{
A *pA = new B();
Handle(pA);
delete pA;
pA = new C();
Handle(pA);
return 0;
}
這個是使用dynamic_cast進行改寫的版本。實際項目中,這種方法會使用的更多點。
RTTI 實現底層實現的原理
簡單的講,是在一個類的虛函數表里面添加了一個新的條目。
這一部分在《深度搜索c++對象模型》里面的3,4,7章有詳細的分析。