Re: Delegation through pure virtual
On May 2, 4:03 am, aksinghdce <aksingh...@gmail.com> wrote:
On May 1, 5:37 pm, James Kanze <james.ka...@gmail.com> wrote:
On May 1, 7:25 pm, aksinghdce <aksingh...@gmail.com> wrote:
Please consider this example from C++ FAQ
{http://www.parashift.com/c++-faq-lite}
class Base {
public:
virtual void foo() = 0;
virtual void bar() = 0;
};
class Der1 : public virtual Base {
public:
virtual void foo();
};
void Der1::foo()
{ bar(); }
class Der2 : public virtual Base {
public:
virtual void bar();
};
class Join : public Der1, public Der2 {
public:
...
};
int main()
{
Join* p1 = new Join();
Der1* p2 = p1;
Base* p3 = p1;
p1->foo();
p2->foo();
p3->foo();
}
Could you please explain how the compiler ( in general ) would
create virtual tables for Der1, Der2 and Join classed.
The same way it creates any virtual table. It determs,
according to the rules of the language, which function should be
called, and puts the address of that function in the vtable.
(Often, it also puts additional information, such as any
corrections needed for the this pointer, in the vtable.)
I really need to understand how this->bar() in function
Der1::foo() gets translated to a call to Der2::bar() call.
The same way any virtual function call is translated.
As per the explanation in 20.4, the Der1's vtable should have a
pointer &Base::bar() because bar() is not implemented in Der1; by what
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