Pure dynamic casts?

Christopher Wright dhasenan at gmail.com
Tue Sep 22 16:09:34 PDT 2009


Jeremie Pelletier wrote:
> You can clearly see all there is to a dynamic cast is simply adjusting 
> the pointer to the object's virtual table. So a compiler knowing the 
> source and destination offset of the vtable can easily inline the code 
> for such a cast.

That may be correct, but you're describing it in a way that confuses me, 
probably in part because it assumes a fair bit of knowledge on the part 
of the listener.

Objects are laid out like this:

class vtbl pointer
object.Object fields
superclass fields
superclass interface1 vtbl pointer
superclass interface2 vtbl pointer
...
class fields
class interface1 vtbl pointer
class interface2 vtbl pointer
...

(The relative order of fields and interface vtbl pointers doesn't matter.)

You need a special vtbl for interfaces because a virtual function call 
works like:
branch (object.vtbl + offset)

The offset must be known at compile time. And it has to be the same 
offset for all possible objects implementing this interface. One 
implemented interface might require an entirely different vtbl layout 
than another. The solution is to use an entirely different vtbl.



More information about the Digitalmars-d mailing list