According to MSDN, __RTDynamicCast() function is used to implement dynamic_cast
in Visual C++. One of its parameters is LONG VfDelta
that is described as "offset of virtual function pointer in object".
AFAIK the vptr
is always located at start of object, so offset will always be zero. I've looked closely at disassembly of various code snippets using dynamic_cast
and I've never seen anything but zero being passed in place of this parameter.
Is vptr
ever located anywhere but the object start? Can this offset be anything but zero?
In case of multiple inheritance there are more then one vptr
and you need the offset
. Take a look here: http://hacksoflife.blogspot.com/2007/02/c-objects-part-3-multiple-inheritance.html
I do not know what Microsoft does, but it's not always true that the vtable pointer is located at offset zero. An example of cases where it may not be is for multiple inheritance (especially if virtual base classes are involved).
Edit:
I'll expand this a bit with examples.
If the first base or a class does not have a vtbl, the derived class will not have a vtbl pointer at offset 0 (such inheritance is bad practice, but is permitted by the language).
If there is a virtual base, the derived class will generally have a pointer to the virtual base at offset 0, not a vtbl pointer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With