• <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 - 297,  comments - 15,  trackbacks - 0
            from http://en.wikipedia.org/wiki/Virtual_inheritance

            Virtual inheritance

            From Wikipedia, the free encyclopedia

            Jump to: navigation, search
            For inheritance of virtual functions, see virtual function.

            In the C++ programming language, virtual inheritance is a kind of inheritance that solves some of the problems caused by multiple inheritance (particularly the "diamond problem") by clarifying ambiguity over which ancestor class members to use. It is used when inheritance is representing restrictions of a set rather than composition of parts. A multiply-inherited base class is denoted as virtual with the virtual keyword.

            Contents

            [hide]

            [edit] The problem

            Consider the following class hierarchy.

            class Animal 
            {
            public:
            virtual void eat();
            };

            class Mammal : public Animal
            {
            public:
            virtual Color getHairColor();
            };

            class WingedAnimal : public Animal
            {
            public:
            virtual void flap();
            };

            // A bat is a winged mammal
            class Bat : public Mammal, public WingedAnimal {};

            Bat bat;

            But how does bat eat()? As declared above, a call to bat.eat() is ambiguous. One would have to call either bat.WingedAnimal::Animal::eat() or bat.Mammal::Animal::eat().

            This situation is sometimes referred to as diamond inheritance because the inheritance diagram is in the shape of a diamond. Virtual inheritance can help to solve this problem.

            [edit] Class representation

            Before going further it is helpful to consider how classes are represented in C++. In particular, inheritance is simply a matter of putting parent and child class one after the other in memory. Thus Bat is really (Animal,Mammal,Animal,WingedAnimal,Bat) which makes Animal duplicated, causing the ambiguity.

            [edit] Solution

            We can redeclare our classes as follows:

            class Animal 
            {
            public:
            virtual void eat();
            };

            // Two classes virtually inheriting Animal:
            class Mammal : public virtual Animal
            {
            public:
            virtual Color getHairColor();
            };

            class WingedAnimal : public virtual Animal
            {
            public:
            virtual void flap();
            };

            // A bat is still a winged mammal
            class Bat : public Mammal, public WingedAnimal {};

            Now the Animal portion of Bat::WingedAnimal is the same Animal as the one used by Bat::Mammal, which is to say that a Bat has only one Animal in its representation and so a call to Bat::eat() is unambiguous.

            This is implemented by providing Mammal and WingedAnimal with a vtable pointer since, e.g., the memory offset between the beginning of a Mammal and of its Animal part is unknown until runtime. Thus Bat becomes (vtable*,Mammal,vtable*,WingedAnimal,Bat,Animal). Two vtable pointers per object, so the object size increased by two pointers, but now there is only one Animal and no ambiguity. There are two vtables pointers: one per inheritance hierarchy that virtually inherits Animal: One for Mammal and one for WingedAnimal. All objects of type Bat will have the same vtable *'s, but each Bat object will contain its own unique Animal object. If another class inherits Mammal, such as Squirrel, then the vtable* in the Mammal object in a Squirrel will be different from the vtable* in the Mammal object in a Bat, although they can still be essentially the same in the special case that the squirrel part of the object has the same size as the Bat part, because then the distance from the Mammal to the Animal part is the same. The vtables are not really the same, but all essential information in them (the distance) is.



            posted on 2009-04-29 22:53 chatler 閱讀(243) 評論(0)  編輯 收藏 引用 所屬分類: C++_BASIS
            <2009年4月>
            2930311234
            567891011
            12131415161718
            19202122232425
            262728293012
            3456789

            常用鏈接

            留言簿(10)

            隨筆分類(307)

            隨筆檔案(297)

            algorithm

            Books_Free_Online

            C++

            database

            Linux

            Linux shell

            linux socket

            misce

            • cloudward
            • 感覺這個博客還是不錯,雖然做的東西和我不大相關,覺得看看還是有好處的

            network

            OSS

            • Google Android
            • Android is a software stack for mobile devices that includes an operating system, middleware and key applications. This early look at the Android SDK provides the tools and APIs necessary to begin developing applications on the Android platform using the Java programming language.
            • os161 file list

            overall

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜

            久久久久久亚洲精品无码| 亚洲中文字幕久久精品无码喷水| 精品久久久久久亚洲精品| 国产综合免费精品久久久| 中文成人久久久久影院免费观看| 狠狠色丁香婷婷久久综合不卡| 日日狠狠久久偷偷色综合96蜜桃| 精品一二三区久久aaa片| 国产精久久一区二区三区| 国内精品久久久久影院优| 国产福利电影一区二区三区,免费久久久久久久精 | 波多野结衣中文字幕久久| 亚洲嫩草影院久久精品| 高清免费久久午夜精品| 久久久久久国产精品无码超碰| 综合久久给合久久狠狠狠97色| 久久久久久午夜成人影院| 久久精品国产99国产精品| 狠狠色丁香婷综合久久| 国产69精品久久久久观看软件| 婷婷久久综合九色综合绿巨人| 久久精品中文字幕无码绿巨人| 久久无码国产| 四虎影视久久久免费观看| 国产成人精品久久一区二区三区 | 久久久无码精品午夜| 久久精品这里热有精品| 成人精品一区二区久久| 99国产欧美久久久精品蜜芽| 久久久www免费人成精品| 亚洲午夜无码久久久久小说| 久久影视综合亚洲| 久久精品无码av| 久久精品国产亚洲Aⅴ蜜臀色欲| 亚洲伊人久久大香线蕉苏妲己| 久久免费国产精品一区二区| 成人国内精品久久久久影院| 97超级碰碰碰久久久久| 亚洲国产精品热久久| 久久久国产精品福利免费| 99久久夜色精品国产网站|