Re: [Solved] sequence of inheritance from virtual base class?
* Richard Damon <Richard@Damon-Family.org>
| The order of inheritance should not matter for conforming code. A
| likely issue is code that assumes &Derived == &Base and does invalid
| casting on it.
Ok, solved it. The reason for the crash was that the base class pointer
was not derived directly from the Derived pointer, but there was another
void* involved:
// virtual base functions omitted for brevity
class Unrelated {};
class Base {};
class Derived : public Unrelated, public Base {};
Base *p1 = &Derived; // ok
// push into hashtable and retrieve it back via void*
hashtable_put("key", &Derived);
void *v = hastable_get("key");
Base *p2 = (Base*) v; // wrong, vtable offset incorrect
Base *p3 = (Derived*) v; // ok, vtable offset correct
The pointer returned by the hashtable_get() needs first to get cast to
the original class pushed into the hashtable, otherwise the compiler
can't calculate the correct vtable offset (obvious once you grok it).
So my original observation that p2 and the original pointer were the
same was indeed the cause of the problem, they must *not* be the same
if Base is not the first base-class of Derived.
Another lesson learned. Had I looked up and posted the *real* code in
the first place instead of trying to simplify, I'm sure the error would
have been obvious (maybe even *before* posting :-)
Thanks
R'
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]