Strange inheritances. I have a bad headache and feel I'm missing something blindingly obvious. Why does the below happen?
class A
{
public:
virtual void func(); //virtual, implemented function
virtual void func2(); //virtual, implemented function
//other declarations, some pure virtuals
};
class B : public A
{
public:
void func(); //reimplemented function
void func2(); //reimplemented function
//other declarations
};
class C : public B
{
public:
void func2() { B::func2(); } //reimplement func2() here as well
//other declarations
};
SomeOtherPlace::callFunc()
{
B *whatever = new C;
whatever->func(); //A::func() called, B::func() expected
whatever->B::func(); //properly calls B::func() without issue
whatever->func2(); //properly calls C::func2()
}
What I'm doing is refactoring some old code written by others, where there are lots of classes (here lumped together as C) inheriting from class A which is in a 3rd party lib, and I'm inserting class B to extract the rather large swathes of code those classes have in common. I reimplemented a function func() from A in B that does not exist in C. I create a C, pointer stored as B* elsewhere and func is called on the B*. I'd expect it to use the function from B's vtable, but it uses the base implementation in A instead.
This is with GCC. I'm pretty sure it would do what I expected in MSVC, which is my usual environment.
Now, if I reimplement func() in C that just forwards the call to B::func(), then it works. Illustrated in the example above as func2(). But means I'll have to add these mock reimplementations in all the C's which is one of the things I was trying to avoid in the first place.
A is abstract, C implements the pure virtuals. B didn't at first, but does now. Same behaviour in either case.
Gah.