Simulating multiple inheritance

Andrej Mitrovic andrej.mitrovich at gmail.com
Sat Mar 31 10:05:06 PDT 2012


This is related to wrapping wxWidgets.

One issue with the new wxWidgets 2.9.x series is that there seem to be
more multiply-inherited classes than before. In particular some of the
main classes have become MI classes (e.g. wxApp derives from
wxAppConsole which is now an MI class). Some wrappers (like wx.net)
maintain these classes manually and simply discard MI altogether and
make every class a singly-inherited class. SWIG does a similar thing
too. E.g. in wx.net wxApp on the C# side looks like this:

C#:
wxApp -> wxEvtHandler -> wxObject -> IDisposable

Whereas on the C++ side things are a bit more complicated:
wxApp -> wxAppConsole -> wxEvtHandler -> wxObject, ...
                                     -> wxEventFilter -> wxObject, ...

So the wxAppConsole subobject is never really accessed in C#. My
dilemma is whether I should to the same thing as C# and figure out
which classes to skip inheriting from (that does mean doing a little
manual-labor), or maybe implement some sort of MI implementation via
interfaces or 'alias this' tricks (the latter is actually quite buggy
atm.).

A sample implementation:
http://paste.pocoo.org/show/574125/

It's not ideal, if the user wants to use a base type he has to type
"IWxEvtHandler" instead of just "wxEvtHandler" like he would in C++.
It's not an issue with naming, I could name the interface "EvtHandler"
and the class itself "wxEvtHandler", but I think people would probably
forget this distinction.

With 'alias this' tricks the user-code can be much cleaner. A sample
implementation:
http://paste.pocoo.org/show/574131/

Unfortunately 'alias this' has plenty of compiler bugs right now.

I guess what I'm looking for is the most convenient implementation for
user-code. Maybe someone else has experience wrapping C++ libraries or
simulating MI? I am *really* not an expert when it comes to MI.


More information about the Digitalmars-d-learn mailing list