Note that there are some explanatory texts on larger screens.

plurals
  1. POMultiple inheritance specify desired virtual function without redfining it
    primarykey
    data
    text
    <p>I want to know if there is a way to specify that in multiple inheritence, one of the base-classes virtual functions becomes the single virtual definition. They only way I see to do this is by writing an explicit definition of this function in the derived class and explicitly call one of the base classes. This is cumbersome, and I was hoping <code>using</code> might help, but it doesn't.</p> <p>For example, in the below code, I have to define a <code>C::value</code> even though I just wish to forward to the class B version. Is there not a simpler way?</p> <pre><code>#include &lt;iostream&gt; using namespace std; struct A { virtual char const * value() const { return "A"; } }; struct B { virtual char const * value() const { return "B"; } }; class C : public A, public B { public: //using B::value; virtual char const * value() const { return B::value(); } }; int main() { C obj; cout &lt;&lt; obj.value() &lt;&lt; endl; A * ptr = &amp;obj; cout &lt;&lt; ptr-&gt;value() &lt;&lt; endl; } </code></pre> <p>Some notes:</p> <ul> <li>in this particular case virtual inheritance of class A is not an option for Class B -- I need a proper construction chain down to the base</li> <li>In theory struct B could also be used unrelated to A, but that is not the case here (in case that provides other options)</li> <li><code>value</code> may be marked as pure virtual if that somehow helps, but I don't believe it does.</li> <li>there are other classes which derive from A but not B</li> <li>to be clear, however "func" is called on C, through any type pointer, must result in "B"</li> </ul> <p>I suspect the answer might be "No", but some confirmation would be good. It feels like the language should support this pattern.</p>
    singulars
    1. This table or related slice is empty.
    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.
 

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