Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can a base class satisfy the definition of a parent's pure virtual function using another parent's function
    primarykey
    data
    text
    <p>I am extending an existing C++ project. I have a base class that derives from two parent classes. One of the parents has a pure virtual function. I want that pure virtual function to be defined by a function implemented in the other parent.</p> <p>So, I want another parent to satisfy the base class's obligation to define a parent's pure virtual function. I have tried two approaches, both have lead to compiler errors.<br> <strong>Any ideas?</strong></p> <p>Here is a C++ program demonstrating my first idea, hoping the compiler would just use <code>base2</code>'s definition of <code>vfunc()</code>.</p> <pre><code>// This is my first approach, hoping the parent base2 of derived would satisfy the need to define // base1's pure virtual vfunc. class base1 { public: virtual int vfunc() = 0; }; class base2 { public: int vfunc() { return 0;} //defined }; class derived : public base1, public base2 { public: //empty }; int main() { derived d; base1 &amp; b1 = d; int result = b1.vfunc(); return result; } </code></pre> <p>The compiler reports that <code>derived</code> is still an abstract class:</p> <pre><code>$ gcc a.cc a.cc: In function ‘int main()’: a.cc:26: error: cannot declare variable ‘d’ to be of abstract type ‘derived’ a.cc:18: note: because the following virtual functions are pure within ‘derived’: a.cc:7: note: virtual int base1::vfunc() </code></pre> <p>Here is my second attempt:</p> <pre><code>// This is my second attempt, defining a vfunc in the derived class that calls the other parent. class base1 { public: virtual int vfunc() = 0; }; class base2 { public: int vfunc() { return 0; } // defined }; class derived : public base1, public base2 { public: int vfunc() { return base2::vfunc(); } // call the other parent's vfunc }; int main() { derived d; base1 &amp; b1 = d; int result = b1.vfunc(); return result; } </code></pre> <p>I actually expected this to do it for me, but instead the linker is giving me a bunch of vtable errors that I do not understand: ( Mac OS 10.6, gcc 4.2.1 )</p> <pre><code>$ gcc inheritance_tester.cc Undefined symbols: "vtable for __cxxabiv1::__vmi_class_type_info", referenced from: typeinfo for derivedin ccmeHq8C.o "___cxa_pure_virtual", referenced from: vtable for base1in ccmeHq8C.o "___gxx_personality_v0", referenced from: _main in ccmeHq8C.o base2::vfunc() in ccmeHq8C.o derived::vfunc() in ccmeHq8C.o base1::base1() in ccmeHq8C.o base2::base2() in ccmeHq8C.o derived::derived()in ccmeHq8C.o CIE in ccmeHq8C.o "vtable for __cxxabiv1::__class_type_info", referenced from: typeinfo for base1in ccmeHq8C.o typeinfo for base2in ccmeHq8C.o ld: symbol(s) not found </code></pre>
    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.
 

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