mechanism this pointer points to &Der2:bar()?
The vptr always points to a vtable corresponding to the most
derived class (once constructors have finished, and before
destructors have started). The vptr in Der1 will point to a
vtable_Der1InJoin. (The compiler may optimize this, if, for
example, vtable_Der1InJoin is identical to the start of
vtable_Join.)
James, thanks for such a nice explanation. But, what I am missing here
is that, in Der1's v-table, pointers to virtual functions will be
pointing to the functions in the base class with the exception of the
functions, which have got their definition in Der1.
You seem to have missed an important point in my comments. Der1
doesn't have a "vtable", it has many "vtables", one for each
most derived class which contains it. When you construct a
Join, the constructor of Join sets up Der1 and Der2's vptr to
point to vtable_Der1InJoin and vtable_Der2InJoin. (In most
implementations, given the hierarchy you've defined,
vtable_Der1InJoin will be a prefix of vtable_Join, and in
Join, Der1 and Join will share the same vptr. This will not be
the case for Der2, however.)
This is actually a bit of a simplification: during construction,
the dynamic type of the object is the type being constructed, so
that during the constructor of Der1, the vptr of Der1 will point
to vtable_Der1, and a call to bar will result in undefined
behavior. But once construction has finished, the vptr's will
all point to the correct vtable.
Now, the scenario is:
Der1's Vtable
pointer1-->Der1::foo(), pointer2-->Base::bar(), .....and other
pointers
That's the vtable of Der1 as most derived class. (Since Der1 is
abstract, it will only be used in the constructor.)
As the definition of bar() is not there in Der1, its v-tables's
pointer should point to the function in Base()
My question is, how is it getting pointed to Der2::bar()???
The constructor of Join set the vptr of Der1 and Der2 to point
to vtable which are valid for Der1 and Der2 which are members of
Join.
--
James Kanze
Thats a real nice explanation. I would like to learn more about this;
so as to let me dive deeper. Thanks a ton :)