Note that there are some explanatory texts on larger screens.

plurals
  1. PODerived class implementing multiple interfaces with a common function signature
    primarykey
    data
    text
    <p>I'm getting a compile error when I try to compile my code. The error is this:</p> <pre><code>multi.cc: In function ‘int main()’: multi.cc:35: error: cannot declare variable ‘mdc’ to be of abstract type ‘MostDerivedClass’ multi.cc:27: note: because the following virtual functions are pure within ‘MostDerivedClass’: multi.cc:13: note: virtual int Interface2::common_func() multi.cc:36: error: request for member ‘common_func’ is ambiguous multi.cc:13: error: candidates are: virtual int Interface2::common_func() multi.cc:21: error: virtual int InterimClass::common_func() </code></pre> <p>And here is my code:</p> <pre><code>class Interface1 { public: virtual int common_func() = 0; virtual ~Interface1() {}; }; class Interface2 { public: virtual int common_func() = 0; virtual int new_func() = 0; virtual ~Interface2() {}; }; class InterimClass : public Interface1 { public: virtual int common_func() { return 10; } }; class MostDerivedClass : public InterimClass, public Interface2 { public: virtual int new_func() { return 20; } }; int main() { MostDerivedClass mdc; int x = mdc.common_func(); cout &lt;&lt; "The value = " &lt;&lt; x &lt;&lt; endl; Interface2 &amp;subset_of_funcs = dynamic_cast&lt;Interface2 &amp;&gt;(mdc); x = subset_of_funcs.common_func(); } </code></pre> <p>My questions:</p> <ul> <li><p>How do I tell the compiler that common_func() is already implemented by the InterimClass which is a base class of MostDerivedClass?</p></li> <li><p>Is there another way to fix my problem? What I would really like to do is to be able to also call common_func from Interface2. I'm working with some legacy code with a huge amount of methods in Interface1. In my new code, I only want to call a small set of these Interface1 functions, plus a few that I need to add.</p></li> </ul>
    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.
    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