• <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.¢%

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

            虛函數與虛繼承的思考

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

            有這么一個關于虛函數和虛繼承的問題,如下:
            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();
            };
            請問sizeof(A), sizeof(B), sizeof(C)分別為多少?

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

            這個看上去沒有什么問題。但是當我把虛繼承去掉以后,這里卻有了一些變化?
            首先,我猜想了一下,A是8,B是16,C是24。
            可惜結果和我想的不一樣,答案是8, 12, 16。很有規律的一個數字。
            從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.


            派生類和基類擁有相同的虛函數表。

            但似乎虛繼承的時候,又擯棄了這一做法。

            所以兩個是有所區別的。

            婷婷五月深深久久精品| 激情久久久久久久久久| 久久精品国产亚洲AV忘忧草18| 亚洲国产成人精品女人久久久| 久久天天日天天操综合伊人av| 伊人久久一区二区三区无码| 一本一本久久a久久综合精品蜜桃 一本一道久久综合狠狠老 | 久久亚洲AV成人无码电影| 日本强好片久久久久久AAA| 狠狠色丁香婷婷综合久久来| 久久精品国产亚洲精品| 亚洲AV乱码久久精品蜜桃| 91精品日韩人妻无码久久不卡| 久久综合鬼色88久久精品综合自在自线噜噜| 狠狠色丁香婷婷久久综合五月| 久久精品国产亚洲AV无码娇色 | 久久免费视频观看| 久久精品一区二区三区AV| 99久久人妻无码精品系列| 亚洲人AV永久一区二区三区久久| 国内精品久久久久伊人av| 综合久久精品色| 久久伊人中文无码| 精品亚洲综合久久中文字幕| 99精品久久精品一区二区| 久久99亚洲综合精品首页| 国产一级做a爰片久久毛片| 国产成人久久精品一区二区三区 | 要久久爱在线免费观看| 青青草原综合久久| 精品久久久久久无码中文字幕一区 | 亚洲国产日韩欧美久久| 久久99精品久久久久久不卡| 国产成年无码久久久久毛片| 亚洲AV无一区二区三区久久| 久久婷婷五月综合国产尤物app| 久久99精品久久久久久9蜜桃| 国产精品狼人久久久久影院| 激情伊人五月天久久综合| 久久久久亚洲AV片无码下载蜜桃 | 久久精品国产久精国产果冻传媒 |