• <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>
            春暖花開
            雪化了,花開了,春天來了
            posts - 149,comments - 125,trackbacks - 0

            有這么一個關于虛函數和虛繼承的問題,如下:
            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.


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

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

            所以兩個是有所區別的。

            posted on 2009-10-07 22:43 Sandy 閱讀(1970) 評論(2)  編輯 收藏 引用 所屬分類: c++學習

            FeedBack:
            # re: 虛函數與虛繼承的思考[未登錄]
            2009-10-07 23:23 | vincent
            orz..您一定看了程序員面試寶典這本書  回復  更多評論
              
            # re: 虛函數與虛繼承的思考
            2010-06-18 09:31 | 小時候可靚了
            虛繼承是個很神奇的東西,貌似不同的編譯器的實現都有區別。但非虛繼承一般都差不多。。 子類、父類共享虛表,子類實現了的虛函數,就重填到虛表中,沒有的就不填,這樣當我們通過父類調用的時候,自然就調用到了。虛繼承則有點麻煩。。。  回復  更多評論
              
            中文字幕亚洲综合久久2| 久久久久亚洲AV无码专区桃色| 久久有码中文字幕| 狠狠色婷婷久久综合频道日韩| 99国产欧美精品久久久蜜芽| 久久久黄片| 国产精品久久99| 中文字幕无码久久精品青草| 久久九九精品99国产精品| 久久久久国产精品嫩草影院| 无码人妻少妇久久中文字幕蜜桃| 久久99精品久久久久久野外| 亚洲欧美成人综合久久久| 99久久亚洲综合精品成人| 浪潮AV色综合久久天堂| 四虎国产精品成人免费久久| 久久91精品国产91久久户| 久久香综合精品久久伊人| 国产69精品久久久久APP下载| 欧美伊香蕉久久综合类网站| 熟妇人妻久久中文字幕| 成人午夜精品无码区久久| 久久精品国产一区二区| 青青青青久久精品国产| 久久精品国产亚洲AV香蕉| 无码专区久久综合久中文字幕| 国产伊人久久| 久久久久久av无码免费看大片| 伊人久久综在合线亚洲2019| 午夜不卡888久久| 国产成人精品久久免费动漫| 久久Av无码精品人妻系列 | 日韩精品久久无码人妻中文字幕 | 精品久久久无码人妻中文字幕豆芽| 亚洲国产成人久久综合野外| 国产福利电影一区二区三区久久久久成人精品综合 | 性欧美丰满熟妇XXXX性久久久| 国产成人精品综合久久久| 亚洲午夜久久久久久久久电影网 | 国产精品美女久久久| 99久久亚洲综合精品网站|