C++ 中的RTTI機制詳解

前言node

RTTI是」Runtime Type Information」的縮寫,意思是運行時類型信息,它提供了運行時肯定對象類型的方法。RTTI並非什麼新的東西,很早就有了這個技術,可是,在實際應用中使用的比較少而已。而我這裏就是對RTTI進行總結,今天我沒有用到,並不表明這個東西沒用。學無止境,先從typeid函數開始講起。ios

typeid函數編程

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

對於C++支持的內建類型,typeid能徹底支持,咱們經過調用typeid函數,咱們就能知道變量的信息。對於咱們自定義的結構體,類呢?函數

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

是的,對於咱們自定義的結構體和類,tpyeid都能支持。在上面的代碼中,在調用完typeid以後,都會接着調用name()函數,能夠看出typeid函數返回的是一個結構體或者類,而後,再調用這個返回的結構體或類的name成員函數;其實,typeid是一個返回類型爲type_info類型的函數。那麼,咱們就有必要對這個type_info類進行總結一下,畢竟它實際上存放着類型信息。ui

type_info類url

去掉那些該死的宏,在Visual Studio 2012中查看type_info類的定義以下:spa

class type_info
{
public:
    virtual ~type_info();
    bool operator==(const type_info& _Rhs) const; // 用於比較兩個對象的類型是否相等
    bool operator!=(const type_info& _Rhs) const; // 用於比較兩個對象的類型是否不相等
    bool before(const type_info& _Rhs) const;
 
    // 返回對象的類型名字,這個函數用的不少
    const char* name(__type_info_node* __ptype_info_node = &__type_info_root_node) const;
    const char* raw_name() const;
private:
    void *_M_data;
    char _M_d_name[1];
    type_info(const type_info& _Rhs);
    type_info& operator=(const type_info& _Rhs);
    static const char * _Name_base(const type_info *,__type_info_node* __ptype_info_node);
    static void _Type_info_dtor(type_info *);
};

  

在type_info類中,複製構造函數和賦值運算符都是私有的,同時也沒有默認的構造函數;因此,咱們沒有辦法建立type_info類的變量,例如type_info A;這樣是錯誤的。那麼typeid函數是如何返回一個type_info類的對象的引用的呢?我在這裏不進行討論,思路就是類的友元函數。.net

typeid函數的使用指針

typeid使用起來是很是簡單的,經常使用的方式有如下兩種:

1.使用type_info類中的name()函數返回對象的類型名稱

就像上面的代碼中使用的那樣;可是,這裏有一點須要注意,好比有如下代碼:

#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同樣,關於這一點,咱們在實際編程中,常常會出錯,必定要謹記。

2.使用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;
}
這裏輸出的結果爲:
I am a B truly.
I am a C truly.

這是一種用法,呆會我再總結如何使用dynamic_cast來實現一樣的功能。

 

dynamic_cast的內幕

在這篇《static_cast、dynamic_cast、const_cast和reinterpret_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;
}

這裏輸出爲:This is class C

在上面代碼中,若是咱們直接將pA賦值給pC,這樣編譯器就會提示錯誤,而當咱們加上了dynamic_cast以後,一切就ok了。那麼dynamic_cast在後面幹了什麼呢?

dynamic_cast主要用於在多態的時候,它容許在運行時刻進行類型轉換,從而使程序可以在一個類層次結構中安全地轉換類型,把基類指針(引用)轉換爲派生類指針(引用)。我在《COM編程——接口的背後》這篇博文中總結的那樣,當類中存在虛函數時,編譯器就會在類的成員變量中添加一個指向虛函數表的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;
}

 

這裏輸出的結果爲:
I am a B truly.
I am a C truly.這個是使用dynamic_cast進行改寫的版本。實際項目中,這種方法會使用的更多點。
相關文章
相關標籤/搜索