• <bdo id='Nj95b'></bdo><ul id='Nj95b'></ul>
  1. <tfoot id='Nj95b'></tfoot>

      <legend id='Nj95b'><style id='Nj95b'><dir id='Nj95b'><q id='Nj95b'></q></dir></style></legend>

      <i id='Nj95b'><tr id='Nj95b'><dt id='Nj95b'><q id='Nj95b'><span id='Nj95b'><b id='Nj95b'><form id='Nj95b'><ins id='Nj95b'></ins><ul id='Nj95b'></ul><sub id='Nj95b'></sub></form><legend id='Nj95b'></legend><bdo id='Nj95b'><pre id='Nj95b'><center id='Nj95b'></center></pre></bdo></b><th id='Nj95b'></th></span></q></dt></tr></i><div id='Nj95b'><tfoot id='Nj95b'></tfoot><dl id='Nj95b'><fieldset id='Nj95b'></fieldset></dl></div>

      <small id='Nj95b'></small><noframes id='Nj95b'>

      虚拟继承如何解决“钻石"问题?(多重继承)歧

      时间:2023-08-02
        <i id='aJeEc'><tr id='aJeEc'><dt id='aJeEc'><q id='aJeEc'><span id='aJeEc'><b id='aJeEc'><form id='aJeEc'><ins id='aJeEc'></ins><ul id='aJeEc'></ul><sub id='aJeEc'></sub></form><legend id='aJeEc'></legend><bdo id='aJeEc'><pre id='aJeEc'><center id='aJeEc'></center></pre></bdo></b><th id='aJeEc'></th></span></q></dt></tr></i><div id='aJeEc'><tfoot id='aJeEc'></tfoot><dl id='aJeEc'><fieldset id='aJeEc'></fieldset></dl></div>

        <small id='aJeEc'></small><noframes id='aJeEc'>

          <legend id='aJeEc'><style id='aJeEc'><dir id='aJeEc'><q id='aJeEc'></q></dir></style></legend>
            <tbody id='aJeEc'></tbody>
          • <bdo id='aJeEc'></bdo><ul id='aJeEc'></ul>

            <tfoot id='aJeEc'></tfoot>

                本文介绍了虚拟继承如何解决“钻石"问题?(多重继承)歧义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                class A                     { public: void eat(){ cout<<"A";} }; 
                class B: virtual public A   { public: void eat(){ cout<<"B";} }; 
                class C: virtual public A   { public: void eat(){ cout<<"C";} }; 
                class D: public         B,C { public: void eat(){ cout<<"D";} }; 
                
                int main(){ 
                    A *a = new D(); 
                    a->eat(); 
                } 
                

                我理解钻石问题,上面的代码没有这个问题.

                I understand the diamond problem, and above piece of code does not have that problem.

                虚拟继承究竟是如何解决问题的?

                How exactly does virtual inheritance solve the problem?

                我的理解:当我说 A *a = new D(); 时,编译器想知道是否可以将 D 类型的对象分配给 A 类型的指针,但它有两条路径可以走,但不能自己决定.

                What I understand: When I say A *a = new D();, the compiler wants to know if an object of type D can be assigned to a pointer of type A, but it has two paths that it can follow, but cannot decide by itself.

                那么,虚拟继承是如何解决这个问题的(帮助编译器做出决定)?

                So, how does virtual inheritance resolve the issue (help compiler take the decision)?

                推荐答案

                您想要:(可通过虚拟继承实现)

                You want: (Achievable with virtual inheritance)

                  A  
                 /   
                B   C  
                  /  
                  D 
                

                而不是:(没有虚拟继承会发生什么)

                And not: (What happens without virtual inheritance)

                A   A  
                |   |
                B   C  
                  /  
                  D 
                

                虚拟继承意味着只有 1 个 A 基类的实例,而不是 2 个.

                Virtual inheritance means that there will be only 1 instance of the base A class not 2.

                您的类型 D 将有 2 个 vtable 指针(您可以在第一个图中看到它们),一个用于 B,一个用于 C他们实际上继承了A.D 的对象大小增加了,因为它现在存储了 2 个指针;但是现在只有一个A.

                Your type D would have 2 vtable pointers (you can see them in the first diagram), one for B and one for C who virtually inherit A. D's object size is increased because it stores 2 pointers now; however there is only one A now.

                所以 B::AC::A 是相同的,所以不会有来自 D 的歧义调用.如果你不使用虚拟继承,你有上面的第二个图.任何对 A 成员的调用都会变得模棱两可,您需要指定要采用的路径.

                So B::A and C::A are the same and so there can be no ambiguous calls from D. If you don't use virtual inheritance you have the second diagram above. And any call to a member of A then becomes ambiguous and you need to specify which path you want to take.

                维基百科有另一个很好的纲要和例子

                这篇关于虚拟继承如何解决“钻石"问题?(多重继承)歧义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:在没有中间光栅的情况下在 OpenGL 中显示 SVG 下一篇:从 C++ STL 容器派生是否有任何真正的风险?

                相关文章

                最新文章

                1. <i id='6vRSY'><tr id='6vRSY'><dt id='6vRSY'><q id='6vRSY'><span id='6vRSY'><b id='6vRSY'><form id='6vRSY'><ins id='6vRSY'></ins><ul id='6vRSY'></ul><sub id='6vRSY'></sub></form><legend id='6vRSY'></legend><bdo id='6vRSY'><pre id='6vRSY'><center id='6vRSY'></center></pre></bdo></b><th id='6vRSY'></th></span></q></dt></tr></i><div id='6vRSY'><tfoot id='6vRSY'></tfoot><dl id='6vRSY'><fieldset id='6vRSY'></fieldset></dl></div>

                  <tfoot id='6vRSY'></tfoot>

                  <legend id='6vRSY'><style id='6vRSY'><dir id='6vRSY'><q id='6vRSY'></q></dir></style></legend>
                      <bdo id='6vRSY'></bdo><ul id='6vRSY'></ul>

                    <small id='6vRSY'></small><noframes id='6vRSY'>