Multiple Inheritance of Classes
JAnderson
ask at me.com
Tue Aug 19 01:06:23 PDT 2008
Denis Koroskin wrote:
>> I think you can do this without multiple inheritance:
>>
>> class MyClass
>> {
>> IntrusiveContainerNode!(MyClass) eventOne, eventTwo;
>> }
>>
>> If you make IntrusiveContainerNode a struct, then there is no extra
>> memory
>> allocation necessary.
>>
>> What is wrong with a solution like that?
>>
>> -Steve
>>
>>
>
> 1) Functionality. You loose an opportunity to iterate over elements of
> the container.
> It can be resolved by extending the IntrusiveContainerNode class to
> store a thisPtr as well (4 more bytes per instance):
>
> // I don't want to have additional memory allocations so it is a struct now
> struct IntrusiveContainerNode(T)
> {
> alias T ValueType;
> package T next;
> package T prev;
> package T thisPtr;
> }
>
> and adding initialization code:
>
> class MyClass
> {
> this()
> {
> eventOne.thisPtr = this;
> eventTwo.thisPtr = this;
> }
>
> IntrusiveContainerNode!(MyClass) eventOne, eventTwo;
> // ...
> }
One technique that's worked well for me is to put the link-list
controller in free functions (or a helper class). The link list looks
like this:
struct ContainerNode(T)
{
T* next;
T* prev;
}
Then the helper has functions like this (note I'm converting from a more
complex C++ so I haven't tested this).
class List(T, alias listName)
{
T* root;
Owner find(...) //Note: this could be a free function if your looking
for something more light weight.
{
T* node = root;
while(node.listName)
{
node = node.listName.Next;
...
}
}
void Add(T* node)
{
...
}
}
//Use
class MyClass
{
ContainerNode(MyClass) eventOne, eventTwo;
}
List!(MyClass, eventOne) eventList1;
List!(MyClass, eventTwo) eventList2;
MyClass c;
eventList1.add(c);
Another way that I've used is bolt-in templates, although I much prefer
the component form as that way you get all the advantages that
components bring to the table.
-Joel
More information about the Digitalmars-d
mailing list