Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>As a practice you should only use virtual inheritance to define interfaces as they are usually used with multiple inheritance to ensure that only one version of the class is present in the derived class. And pure interfaces are the safest form of multiple inheritance. Of course if you know what you are doing you can use multiple inheritance as you like, but it can result in brittle code if you are not careful.</p> <p>The biggest drawback with virtual inheritance is if their constructors take parameters. If you have to pass parameters to the constructor of a virtual base class you force all derived classes to explicitly call the constructor (they cannot rely on a base class calling the constructor).</p> <p>The only reason I can see for your explicit advise is that data in your virtual base class these might require constructor parameters.</p> <p><strong>Edit</strong> I did some home work after Martin's comment, thank Marin. The first line is not quite true:</p> <blockquote> <p>As a practice you should only use virtual inheritance to define interfaces as they are usually used with multiple inheritance to ensure that only one version of the class is present in the derived class.</p> </blockquote> <p>Virtual inheritance makes no difference if the base class is a pure interface (except for slightly different compiler errors, in vc8, if all the methods are not implemented). It only makes a real difference if the base class has data, in this case you end up with a diamond rather than a U shape</p> <pre><code>Non virtual virtual A A A | | / \ B C B C \ / \ / D D </code></pre> <p>In the virtual case B and C share the same copy of A.</p> <p>However I still agree with everything else about pure interfaces being the safest form of multiple inheritance, even if they don't require virtual inheritance. And the fact that constructor parameters and virtual inheritance are a pain.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload