Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h2>Inheriting twice</h2> <p>With double inheritance you have an ambiguity - the compiler cannot know which of the two A bases do you want to use. If you want to have two A bases (sometimes you may want to do this), you may select between them by casting to B or C. The most appropriate from default casts here is the <code>static_cast</code> (as the weakest available), however it is not realy needed (it is still stronger than your case needs), as you are not casting to a derived type. A custom <code>safe_cast</code> template should do the job:</p> <pre><code>/// cast using implicit conversions only template &lt;class To,class From&gt; inline To safe_cast( const From &amp;from ) {return from;} main() { D obj; foo(safe_cast&lt;B *&gt;(&amp;obj)); //error. How do i call with D's B part. } </code></pre> <h2>Compile time types - use templates</h2> <blockquote> <p>Also, in case of virtual inheritance, why the offset information need to be stored in the vtable. This can be determined at the compile time itself. In the above case, if we pass foo with D's object, at compile time only we can calculate the offset of D's A part.</p> </blockquote> <p>This is a misconception. The foo function as it is written now has no compile type information about ptr type other than it is A *, even if you pass B * or C*. If you want foo to be able to act based on the type passed compile time, you need to use templates:</p> <pre><code>template &lt;class TypeDerivedFromA&gt; int foo(TypeDerivedFromA *ptr) { ptr-&gt;eat(); } </code></pre> <h2>Virtual Inheritance</h2> <p>Your questions mentions virtual inheritance. If you want to use virtual inheritance, you need to specify so:</p> <pre><code>class B: public virtual A ... class C: public virtual A ... </code></pre> <p>With this the code would compile, but with this solution there is no way you could select between B::A or C::A (there is only one A), therefore this is probably not what you are about.</p> <h2>Virtual functions</h2> <p>Furthermore, your questions seems to be confusing two different concepts, virtual inheritance (which means sharing one base class between two intermediate base classes) and virtual functions (which mean allowing derived class function to be called via base class pointer). If you want the B::eat to be called using A pointer, you can do this without virtual inheritance (actually virtual inheritance would prevent you doing so, as explained above), using virtual functions:</p> <pre><code>class A { int a; int b; public: virtual void eat() { cout&lt;&lt;"A::eat()"&lt;&lt;endl; } }; </code></pre> <p>If virtual functions are not acceptable for you, the compile time mechanism for this are templates, as explained above.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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