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

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

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

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

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

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

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

            FeedBack:
            # re: 虛函數(shù)與虛繼承的思考[未登錄]
            2009-10-07 23:23 | vincent
            orz..您一定看了程序員面試寶典這本書  回復(fù)  更多評論
              
            # re: 虛函數(shù)與虛繼承的思考
            2010-06-18 09:31 | 小時候可靚了
            虛繼承是個很神奇的東西,貌似不同的編譯器的實(shí)現(xiàn)都有區(qū)別。但非虛繼承一般都差不多。。 子類、父類共享虛表,子類實(shí)現(xiàn)了的虛函數(shù),就重填到虛表中,沒有的就不填,這樣當(dāng)我們通過父類調(diào)用的時候,自然就調(diào)用到了。虛繼承則有點(diǎn)麻煩。。。  回復(fù)  更多評論
              
            亚洲国产成人精品无码久久久久久综合 | 久久国产精品77777| 精品伊人久久久| 午夜精品久久久久久久| 国产精品久久久久久久| 欧美久久久久久午夜精品| 少妇人妻88久久中文字幕| 品成人欧美大片久久国产欧美... 品成人欧美大片久久国产欧美 | 亚洲精品无码专区久久同性男| 国产69精品久久久久久人妻精品| 久久这里只精品国产99热| 中文成人无码精品久久久不卡 | 久久精品亚洲欧美日韩久久| 久久精品日日躁夜夜躁欧美| 91精品久久久久久无码| 亚洲精品乱码久久久久久中文字幕| 伊人久久综合热线大杳蕉下载| 亚洲国产精品无码久久SM| 久久男人中文字幕资源站| 久久综合久久综合久久| 亚洲精品国产字幕久久不卡| 久久久久国产一级毛片高清板| 狠狠色噜噜狠狠狠狠狠色综合久久| 日本加勒比久久精品| 蜜桃麻豆www久久| 国内精品九九久久久精品| 亚洲成色WWW久久网站| 亚洲国产成人精品91久久久| 久久久中文字幕| 久久精品一区二区三区不卡| 久久人人爽人人爽人人片av高请| 久久亚洲熟女cc98cm| 久久中文精品无码中文字幕| 一本久久a久久精品综合夜夜 | 欧洲精品久久久av无码电影 | 99久久精品免费看国产一区二区三区| 少妇久久久久久久久久| 久久精品国产亚洲av日韩| 久久人人爽人人爽人人AV| 久久久一本精品99久久精品88| 性欧美丰满熟妇XXXX性久久久|