"multi morphic" classes

pragma eric.t.anderton at gmail.com
Thu May 1 20:28:00 PDT 2008


Russell Lewis wrote:
> I apologize if I'm missing some subtlety, but it seems to me that this 
> would be pretty straightforward...
> 
> How about just building a class by hand using a struct?  You declare  a 
> struct type which contains a bunch of function pointers, and each 
> instance struct has a pointer to that common vtbl.  Then you write 
> wrappers as member functions of the struct, which call the vtbl 
> functions, pass "this" as the first argument.

BCS pretty much said it all.  However, I would like to mention that 
there is one thing that can be accomplished via this technique: 
interceptors/decorators.  That in turn opens the door for a kind of AOP 
(without point-cuts obviously).

Anyway, by implementing the a vtbl in plain code, you have the ability 
to manipulate it on the fly.  Were each instance allowed to have it's 
own personal copy of that table, all kinds of modifications become 
possible.  Informal decorators are just one kind of thing that's do-able 
in such a scheme.

A practical example of a language that supports this is ECMAScript 
(javascript).  You can modify a "class" prototype's methods, or muck 
about with any given instance's methods in this way.  It's handy if you 
want to eavesdrop on just one instance of an object (insert debug code 
on the fly), or want to change the behavior of a whole class of objects 
while the system is running.

- Pragma



More information about the Digitalmars-d mailing list