Multiple Inheritance of Classes

Chris R. Miller lordSaurontheGreat at gmail.com
Wed Aug 13 15:50:00 PDT 2008


Denis Koroskin wrote:
> I use MI often and have positive experience with it. One good pattern 
> that I use is the Intrusive container.
> Suppose you have an item that you want to store in a list. 
> Unfortunately, putting stuff into the single- or double-linked list 
> leads to a memory allocation (unless some pool is used). Sometimes it is 
> desirable to put next and prev elements into the item itself, so that no 
> memory allocation is ever needed. Besides, now you can easily say 
> whether an item is stored in any container. This can be implemented via 
> inheritance:
> 
> class IntrusiveContainerNode(T)
> {
>     alias T ValueType;
>     package T next;
>     package T prev;
> }
> 
> class IntrusiveContainer(TNode)
> {
>     alias TNode.ValueType ValueType;
> 
>     void add(ValueType value);
>     void remove(ValueType value);
> }
> 
> class MyClass : public Node!(MyClass)
> {
>    // ...
> }
> 
> This imposes the restriction that an item can be stored in 1 container 
> at a time. However, you can subclass twise in order to be storable in 
> different containers:
> 
> typedef EventOneListener IntrusiveContainerNode;
> typedef EventTwoListener IntrusiveContainerNode;
> 
> class MyClass : EventOneListener!(MyClass), EventTwoListener!(MyClass)
> {
>     // ...
> }
> 
> MyClass instance = new MyClass();
> eventOneListeners.add(instance);
> eventTwoListeners.add(instance);
> 
> It is currently impossible to implement this approach using mixins (due 
> to a bug I'm yet to submit).

Chris E. implemented something similar to that using mixins:

http://www.dprogramming.com/list.php

It works fairly well.  Your example of typdefing to support multiple 
lists is pretty cool though :^)

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 258 bytes
Desc: OpenPGP digital signature
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20080813/60e423e3/attachment.pgp>


More information about the Digitalmars-d mailing list