• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            統計

            • 隨筆 - 50
            • 文章 - 42
            • 評論 - 147
            • 引用 - 0

            留言簿(6)

            隨筆分類

            文章分類

            Link

            搜索

            •  

            積分與排名

            • 積分 - 166405
            • 排名 - 159

            最新評論

            閱讀排行榜

            評論排行榜

            對象與對象的類型信息----獲取對象的RTTI信息

            夢在天涯 在上篇文章里問可不可以研究下對象與對象的類型信息怎么聯系起來的,就是當調用dynamic_cast的時候為什么能夠正確的識別對象的類型

            RTTI(Run Time Type Identification)運行時類型識別是有編譯器在編譯器生成的特殊類型信息,包括對象繼承關系,對象本身的描述,RTTI是為多態而生成的信息,所以只有具有虛函數的對象在會生成

            那RTTI在哪里呢?
            MSVC編譯器在vftable前設置了一個指針,指向叫做“Complete Object Locator”(完整對象定位器)的結構。這樣稱呼是因為它允許編譯器從特定的vftable指針(因為一個類可能有若干vftable)找到完整對象的位置。COL就像如下定義:

            請看如下代碼:

              1#include "iostream"
              2#include "string"
              3
              4
              5using namespace std;
              6class Aclass
              7{
              8public:
              9     int a;
             10    virtual void setA(int tmp)
             11    {
             12        a=tmp;
             13        cout<<a<<endl;
             14    }

             15}
            ;
             16class Bclass:public Aclass
             17{
             18public:
             19    virtual void setA(int tmp)
             20    {
             21        a=tmp+10;
             22        cout<<a<<endl;
             23    }

             24public:
             25    void print()
             26    {
             27        cout<<a<<endl;
             28    }

             29}
            ;
             30class Cclass:public Bclass
             31{
             32}
            ;
             33typedef unsigned long DWORD;
             34struct TypeDescriptor
             35{
             36    DWORD ptrToVTable;
             37    DWORD spare;
             38    char name[8];
             39}
            ;
             40struct PMD
             41{
             42
             43    int mdisp;  //member displacement
             44
             45    int pdisp;  //vbtable displacement
             46
             47    int vdisp;  //displacement inside vbtable
             48
             49}
            ;
             50struct RTTIBaseClassDescriptor
             51
             52{
             53
             54    struct TypeDescriptor* pTypeDescriptor; //type descriptor of the class
             55
             56    DWORD numContainedBases; //number of nested classes following in the Base Class Array
             57
             58    struct PMD where;        //pointer-to-member displacement info
             59
             60    DWORD attributes;        //flags, usually 0
             61
             62}
            ;
             63
             64struct RTTIClassHierarchyDescriptor
             65{
             66
             67    DWORD signature;      //always zero?
             68
             69    DWORD attributes;     //bit 0 set = multiple inheritance, bit 1 set = virtual inheritance
             70
             71    DWORD numBaseClasses; //number of classes in pBaseClassArray
             72
             73    struct RTTIBaseClassArray* pBaseClassArray;
             74
             75}
            ;
             76
             77struct RTTICompleteObjectLocator
             78
             79{
             80
             81    DWORD signature; //always zero ?
             82
             83    DWORD offset;    //offset of this vtable in the complete class
             84
             85    DWORD cdOffset;  //constructor displacement offset
             86
             87    struct TypeDescriptor* pTypeDescriptor; //TypeDescriptor of the complete class
             88
             89    struct RTTIClassHierarchyDescriptor* pClassDescriptor; //describes inheritance hierarchy
             90
             91
             92}
            ;
             93
             94
             95int _tmain(int argc, _TCHAR* argv[])
             96{
             97    Aclass* ptra=new Bclass;
             98    int ** ptrvf=(int**)(ptra);
             99    RTTICompleteObjectLocator str=
            100        *((RTTICompleteObjectLocator*)(*((int*)ptrvf[0]-1)));
            101    //abstract class name from RTTI
            102    string classname(str.pTypeDescriptor->name);
            103    classname=classname.substr(4,classname.find("@@")-4);
            104    cout<<classname<<endl;
            105    system("pause");
            106    return 0;
            107}

            108

            輸出結果:


            在RTTI運行時結構體中包含許多豐富的信息,甚至我們可以利用一個實例的RTTI信息去復原整個類繼承圖譜

            而對于dynamic_cast也是利用這個信息來準確的識別實例所對應的類型,不過如果對于沒有多態的實例,dynamic_cast所做的也只是和編譯器類型轉換一樣的事情,僅僅是通過類型和繼承關系進行轉換,還是看例子吧:

             1class mother
             2{
             3    int a;
             4    int b;
             5}
            ;
             6class father
             7{
             8    double c;
             9}
            ;
            10class son:public mother,public father
            11{
            12    char a;
            13    short c;
            14}
            ;
            15
            16int _tmain(int argc, _TCHAR* argv[])
            17{
            18    mother* m=new son();
            19    father* f=dynamic_cast<father*>(m);
            20    system("pause");
            21    return 0;
            22}
            運行會得到以下錯誤:
            error C2683: “dynamic_cast”:“mother”不是多態類型
            修改如下
            class mother
            {
             int a;
             int b;
             virtual void fun()
             {
             }
            };
            一切正常

            就說這么多吧,歡迎多交流

            posted on 2009-03-12 17:55 pear_li 閱讀(4244) 評論(3)  編輯 收藏 引用 所屬分類: C++

            評論

            # re: 對象與對象的類型信息----獲取對象的RTTI信息 2009-03-12 21:32 夢在天涯

            超好啊,原來真的在前面啊,看到C++對象模型說的,但是我確實是沒有想到原來是在VTable - 1??!
              回復  更多評論    

            # re: 對象與對象的類型信息----獲取對象的RTTI信息 2009-03-13 00:19 pear_li

            @夢在天涯
            呵呵,是啊,夢在天涯的確是看了好多書啊
              回復  更多評論    

            # re: 對象與對象的類型信息----獲取對象的RTTI信息 2010-08-22 00:19 tomren

            不錯,剛看到:)
              回復  更多評論    
            色婷婷久久综合中文久久一本| 精品国产VA久久久久久久冰 | 99久久国产综合精品麻豆| 国产69精品久久久久APP下载| 66精品综合久久久久久久| 久久精品国产亚洲av高清漫画| 中文精品久久久久人妻不卡| 热99RE久久精品这里都是精品免费| 日批日出水久久亚洲精品tv| 日本国产精品久久| 久久久久久国产精品美女 | 亚洲欧洲久久av| 午夜精品久久久久| 性做久久久久久久久老女人| 久久乐国产综合亚洲精品| 国产精品中文久久久久久久| 久久久久久久久波多野高潮| 无码人妻精品一区二区三区久久久 | 婷婷久久综合九色综合九七| 亚洲欧美久久久久9999| 97久久婷婷五月综合色d啪蜜芽| 久久亚洲国产成人精品性色| 久久国产精品成人免费| 久久激情亚洲精品无码?V| 久久人人爽人人爽人人片AV东京热 | 亚洲国产成人久久笫一页| 欧美日韩精品久久久久| 亚洲中文字幕无码久久精品1| 久久久亚洲欧洲日产国码aⅴ | 国产精品永久久久久久久久久| 色综合久久夜色精品国产| 久久综合香蕉国产蜜臀AV| 91精品国产综合久久四虎久久无码一级| 久久久久亚洲精品无码网址| 亚洲国产美女精品久久久久∴| 国产精品成人精品久久久| 一本色综合网久久| 久久一区二区三区免费| 国产精品毛片久久久久久久| 热99RE久久精品这里都是精品免费 | 久久久国产精品亚洲一区|