I am expanding an existing C ++ project. I have a base class that comes from two parent classes. One of the parents has a pure virtual function. I want this pure virtual function to be defined by a function implemented in another parent.
So, I want the other parent to satisfy the obligation of the base class to define the parent pure virtual function. I tried two approaches, both led to compiler errors.
Any ideas?
Here is C ++, a program demonstrating my first idea, hoping the compiler just uses the definition base2 vfunc().
class base1 {
public:
virtual int vfunc() = 0;
};
class base2 {
public:
int vfunc() { return 0;}
};
class derived : public base1, public base2 {
public:
};
int main()
{
derived d;
base1 & b1 = d;
int result = b1.vfunc();
return result;
}
, derived - :
$ 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()
:
class base1 {
public:
virtual int vfunc() = 0;
};
class base2 {
public:
int vfunc() { return 0; }
};
class derived : public base1, public base2 {
public:
int vfunc() { return base2::vfunc(); }
};
int main()
{
derived d;
base1 & b1 = d;
int result = b1.vfunc();
return result;
}
, , vtable, : (Mac OS 10.6, gcc 4.2.1)
$ 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