Multiple Inheritance of Classes

Denis Koroskin 2korden at gmail.com
Thu Aug 14 05:38:27 PDT 2008


On Thu, 14 Aug 2008 02:50:00 +0400, Chris R. Miller  
<lordSaurontheGreat at gmail.com> wrote:

> 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 :^)
>

Well, yes, it uses the same idea, but implementation is different. It has  
a few problems, though: an item is limited to a single container (I can do  
the same with single inheritance) and is a list itself (although it is  
useful in some cases, it is undesired in general).

I'll post my implementation with mixins soon (today, hopefully).



More information about the Digitalmars-d mailing list