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

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

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

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

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

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

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

            FeedBack:
            # re: 虛函數(shù)與虛繼承的思考[未登錄]
            2009-10-07 23:23 | vincent
            orz..您一定看了程序員面試寶典這本書  回復  更多評論
              
            # re: 虛函數(shù)與虛繼承的思考
            2010-06-18 09:31 | 小時候可靚了
            虛繼承是個很神奇的東西,貌似不同的編譯器的實現(xiàn)都有區(qū)別。但非虛繼承一般都差不多。。 子類、父類共享虛表,子類實現(xiàn)了的虛函數(shù),就重填到虛表中,沒有的就不填,這樣當我們通過父類調用的時候,自然就調用到了。虛繼承則有點麻煩。。。  回復  更多評論
              
            国产精品久久成人影院| 久久久久国产精品人妻| 99久久精品这里只有精品| 久久最近最新中文字幕大全 | 亚洲AV日韩精品久久久久| 国产成年无码久久久久毛片| 伊人热人久久中文字幕| 久久久久久久国产免费看| 亚洲精品无码久久一线| 韩国三级中文字幕hd久久精品 | 伊人久久久AV老熟妇色| 97久久综合精品久久久综合| 亚洲欧美成人久久综合中文网| 欧美黑人激情性久久| 激情综合色综合久久综合| 亚洲va久久久噜噜噜久久天堂| 国产激情久久久久影院小草| 久久精品国产亚洲AV无码娇色| 国产高清美女一级a毛片久久w| 人妻精品久久久久中文字幕一冢本| 国产99久久久久久免费看 | 99久久精品这里只有精品| 亚洲AV无码久久精品蜜桃| 青青热久久国产久精品 | 国产高潮久久免费观看| 久久水蜜桃亚洲av无码精品麻豆| 精品久久久久久无码中文字幕| 99久久国产热无码精品免费| 久久精品国产免费观看三人同眠| 亚洲国产婷婷香蕉久久久久久| 久久成人18免费网站| 97精品国产97久久久久久免费| 久久国产乱子伦免费精品| av色综合久久天堂av色综合在| 性做久久久久久久久浪潮| 久久免费视频一区| 人妻无码精品久久亚瑟影视| 久久久久亚洲av成人无码电影| 人妻无码精品久久亚瑟影视| 久久免费视频1| 一本色道久久88精品综合|