one counter argument kai:
the compiler can determine at the compile time if the class of the
object that is being created on the stack contains the virtaul function
and hence CAN put a pointer to vtable at the end of the object. after
all only a single pointer to the vtable needs to be put in to the
object. so the size of the objects of this class can be determinded at
the compile time.
so the question still remains:
why polymorphism dosn't work with objects in c++.
thanks
rt
You either have value semantics, by directly accessing the object, or
you have reference semantics, through a pointer or reference.
In the case of a pointer or reference, you have a static type which
maybe of a sub-object (base class) type and the dynamic type which is
always the most derived type. If the pointer is already of the most
derived type of the object, the static and dynamic type of the object
are the same. Calls to virtual functions will still be dispatched
dynamically at run-time (the compiler doesn't know if the static and
dynamic type are the same) but you won't end up calling a more derived
version of a function because you are already pointing to the most
derived object. So although polymorphism is enabled, in the above case,
it never comes into play.
In the case where you have value semantics, the static and the dynamic
type are always the same and the type will always be the most derived
object. The difference here to the scenario outlined above is that the
compiler knows that the static and dynamic types are the same and can
therefore work out the correct function to call at compile-time. Again
polymorphism doesn't come into play.
So, polymorphism can only *work* when the static and dynamic types are
different and this is never the case when using value semantics.