sequence of inheritance from virtual base class?
I have a problem with the sequence of inheritance:
// header1.h
class Base {
public:
Base() {}
virtual ~Base() {}
virtual void foo() = 0;
};
// header2.h
#include "header1.h"
class Unrelated1 {};
class Unrelated2 {};
class Derived1 : public Base, public Unrelated1, public Unrelated2 {
public:
Derived1() {}
~Derived1() {}
void foo() {}
};
class Derived2 : public Unrelated1, public Unrelated2, public Base {
public:
Derived2() {}
~Derived2() {}
void foo() {}
};
// main.cc
// g++ -o main main.cc
#include "header2.h"
int main() {
Derived1 d1;
Derived2 d2;
d1.foo(); // ok
d2.foo(); // crashes in big app
}
The problem seems to be the sequence of inheritance: if I inherit from
'Base' first (Derived1), the code seems to run ok. If I inherit from
'Base' last (Derived2), the code crashes reliably at runtime in a
seemingly unrelated code position (other class, other function, but
always the same).
The crash happens when Derived2 comes from one shared lib, Derived1 from
another. Calling a non-virtual function in Base seems ok, the crash
happens only when calling a virtual function in Base, so it looks to me
like a wrong lookup in the virtual function table.
This happens on Windows with Microsoft cl and on Linux with g++.
I've checked the FAQ for multiple inheritance issues but found nothing
related to this. It also does not matter whether I use
Derived2 : ... public Base ...
or
Derived2 : ... virtual public Base ...
Shouldn't this code be independent on the sequence of inheritance?
Thanks for any insights...
R'
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]