Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is vptr ever located not at start of object?

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?

like image 491
sharptooth Avatar asked Sep 16 '25 18:09

sharptooth


2 Answers

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

like image 93
Andrew Avatar answered Sep 18 '25 10:09

Andrew


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.

like image 39
Analog File Avatar answered Sep 18 '25 09:09

Analog File