• <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>

            S.l.e!ep.¢%

            像打了激速一樣,以四倍的速度運(yùn)轉(zhuǎn),開心的工作
            簡(jiǎn)單、開放、平等的公司文化;尊重個(gè)性、自由與個(gè)人價(jià)值;
            posts - 1098, comments - 335, trackbacks - 0, articles - 1
              C++博客 :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

            虛函數(shù)與虛繼承的思考

            Posted on 2009-10-08 00:42 S.l.e!ep.¢% 閱讀(201) 評(píng)論(0)  編輯 收藏 引用 所屬分類: C++

            有這么一個(gè)關(guān)于虛函數(shù)和虛繼承的問題,如下:
            class A
            {
            ??? char k[3];
            public:
            ??? virtual void aa();
            };

            class B: public virtual A
            {
            ??? char j[3];
            public:
            ??? virtual void bb();
            };

            class C: public virtual B
            {
            ??? char i[3];
            public:
            ?? virtual void cc();
            };
            請(qǐng)問sizeof(A), sizeof(B), sizeof(C)分別為多少?

            對(duì)于A, 我們很清楚的知道,其大小為8。
            對(duì)于B,考慮到虛繼承和自身的虛函數(shù),我們也可以算出來起大小為8+8+4 = 20
            對(duì)于C,其大小為20+8+4 = 32。
            其中 4為虛繼承所占用的指針。

            這個(gè)看上去沒有什么問題。但是當(dāng)我把虛繼承去掉以后,這里卻有了一些變化?
            首先,我猜想了一下,A是8,B是16,C是24。
            可惜結(jié)果和我想的不一樣,答案是8, 12, 16。很有規(guī)律的一個(gè)數(shù)字。
            從A到B,只增加了4。什么原因呢?

            http://www.diybl.com/course/3_program/c++/cppjs/2007927/74925.html這里介紹了一些

            The existence of virtual function(s)

            Existence of virtual function(s) will add 4 bytes of virtual table pointer in the class, which will be added to size of class. Again, in this case, if the base class of the class already has virtual function(s) either directly or through its base class, then this additional virtual function won't add anything to the size of the class. Virtual table pointer will be common across the class hierarchy. That is

            class Base {

            public:

            ?...????????

            virtual void SomeFunction(...);

            private:??

            int iAMem

            };

            class Derived : public Base

            {

            ?...???????

            ?virtual void SomeOtherFunction(...);

            private:??????

            int iBMem

            };

            In the example above, sizeof(Base) will be 8 bytes--that is sizeof(int iAMem) + sizeof(vptr). sizeof(Derived) will be 12 bytes, that is sizeof(int iBMem) + sizeof(Derived). Notice that the existence of virtual functions in class Derived won't add anything more. Now Derived will set the vptr to its own virtual function table.


            派生類和基類擁有相同的虛函數(shù)表。

            但似乎虛繼承的時(shí)候,又?jǐn)P棄了這一做法。

            所以兩個(gè)是有所區(qū)別的。

            国产精品九九久久免费视频 | 久久精品国产AV一区二区三区| 久久久精品久久久久特色影视| 亚洲国产成人久久精品99| 99久久精品免费看国产一区二区三区 | 久久精品国产亚洲Aⅴ香蕉| 亚洲精品无码久久久久久| 99久久婷婷国产一区二区| 久久人人爽人人爽人人片AV麻烦| 国产亚洲欧美成人久久片| 久久夜色精品国产噜噜亚洲a| 久久99免费视频| 99久久国产宗和精品1上映| 久久精品国产欧美日韩| 91精品国产综合久久久久久| 三级韩国一区久久二区综合| 久久99中文字幕久久| 久久丫精品国产亚洲av| 日本五月天婷久久网站| 青青久久精品国产免费看| 久久综合丁香激情久久| 999久久久免费精品国产| 久久久久人妻一区精品色| 久久久久久精品无码人妻| 一本久久综合亚洲鲁鲁五月天亚洲欧美一区二区 | 久久综合九色综合久99| 久久久女人与动物群交毛片| 久久精品国产亚洲av麻豆蜜芽 | 久久国产欧美日韩精品| 色综合久久中文字幕无码 | 亚洲一区中文字幕久久| 99久久久精品免费观看国产| 国产成人久久精品一区二区三区| 一本色道久久综合亚洲精品| 99久久精品免费看国产一区二区三区 | 久久久这里有精品中文字幕| 国产成人久久精品麻豆一区| 精品久久久无码中文字幕| 国产精品伊人久久伊人电影 | 国产午夜精品理论片久久| 久久人人爽人人爽人人片AV东京热 |