Michael said:
Peter said:
Hello,
I am not sure. But is there not a certain table in each binary which
stores the inheritance??
Hi,
It's up to the implementation, but in Stroustrup's book The C++ Programming
Language he describes a possible scheme (probably used by most compilers)
where there is an array of function pointers called _vtab[] (if I remember
correctly), which point to the inherited functions.
Names vary, but the implementation is a common one.
It works especially well in the presence of single
non-virtual inheritance. However, it's not the only
possible mechanism, and other mechanisms /must/ be used
for virtual inheritance.
For example: Someone calls a funtion abc() of a object A. But class A
does not implement this method. The method is implemented by a
superclass B.
In this case _vtab will just have one entry:
_vtab[0] = (the address of) B:abc()
Perhaps. Another possibility is that the compiler knows
at compile time that you're working on an A, that A has
no abc so you must mean B::abc. In that case it will just
substitute a call to B::abc(). If that's the only call to
abc(), it doesn't even have to appear in the vtable
It's been a long time, but I seem to remember that there is one vtab for
each virual function in each object.
One vtable entry, that is, and per class (not object).
Again, the same disclaimers (not universal, especially
not for complex cases, not when optimized out) apply.
If A did override abc, then there would be two entries in the array.
No. There would be two vtables, each with one entry. Each
object would point to the correct one, set by the ctor.
So if you construct an A, you get the A vtable and A::abc()
even if you use a B* pointer to the A.
(and this all aplies to virtual functions only, hence the
v in vtable.)
You might want to look at "typeid".
HTH,
Michiel Salters