Sojin
Can this [abstract] class CTemp have a v-table?
Note that vtables are implementation artifacts. They are not defined by
the C++ language.
Class Ctemp cannot have an object of his type since it is virtual,
hence if there is no class there is no v-table?
There certainly is a class. You might think that since CTemp cannot be
instantiated by itself, there is no need for a vtable. However, during
the construction of a CDerived object, there is a brief moment during
which the object is considered to be a CTemp, and is not yet a
CDerived.
That happens when the CTemp constructor has been entered, but the
CDerived constructor has not yet been. During this time, if virtual
functions are called, they have to go to CTemp definitions.
This cannot be implemented statically, because a constructor can run
arbitrary code which doesn't know what type the object is:
void LibraryFunction(CTemp *basePtr)
{
basePtr->virtualFunction();
}
CTemp::CTemp()
{
LibraryFunction(this);
}
The virtualFunction() call in LibraryFunction has to
CTemp::virtualFunction even though the object is really a CDerived, and
there is a CDerived::virtualFunction.
LibraryFunction cannot do this by clairvoyance: it has to retrieve some
type-related information from the object, such as a vtable pointer.
Even if CTemp is a pure abstract base class (pure virtuals only) so
that the call to virtualFunction is actually a pure virtual call error,
there still has to be something in the object to detect that run-time
error.
Quite conceivably, if the base class has nothing but pure virtuals,
perhaps the vtable could be optimized away. The vtable pointer in the
partially constructed object could just be set to some special value
which summarizes the idea that the object is a pure ABC. Of course,
the code generated for virtual calls would then have to check for this
value to detect pure virtual calls.
(Null would be a poor choice for this value, because, then the run-time
system would not be able to provide a diagnostic which distinguishes
between a pure virtual call
error, and a virtual call on an object that has been stomped-over with
zero bytes).
In a sense, that special value could still be considered a vtable,
albeit a compressed, summarized one.