Getting rid of dynamic polymorphism and classes

Marco Leise Marco.Leise at gmx.de
Sat Nov 10 01:23:23 PST 2012


Am Thu, 08 Nov 2012 23:38:53 +0100
schrieb "Tommi" <tommitissari at hotmail.com>:

> On Thursday, 8 November 2012 at 21:43:32 UTC, Max Klyga wrote:
> > Dinamic polimorphism isn't gone anywhere, it was just shifted 
> > to delegates.
> 
> But there's no restrictive type hierarchy that causes unnecessary 
> coupling. Also, compared to virtual functions, there's no 
> overhead from the vtable lookup. Shape doesn't need to search for 
> the correct member function pointer, it already has it.
> 
> It's either that, or else I've misunderstood how virtual 
> functions work.

They work like this: Each object has as a pointer to a table
of method pointers. When you extend a class, the new method
pointers are appended to the list and existing entries are
replaced with overrides where you have them.
So a virtual method 'draw()' may get slot 3 in that table and
at runtime it is not much more than:

obj.vftable[3]();

These are three pointer dereferences (object, vftable entry
3, method), but no search.

-- 
Marco



More information about the Digitalmars-d mailing list