A Tricky C++ Multiple Inheritance Problem

Started by
4 comments, last by Geometrian 11 years, 2 months ago

Hi,

I have two classes in a library (can't be changed) named "LibBase" and "LibChild". I have three classes that I am writing named "AppBase", "AppChild1", "AppChild2".

Other classes need "AppBase" to be treatable like a "LibBase", yet this class is instantiated through "AppChild1" and "AppChild2", which provide special implementations.

Consequently, I have organized the classes into the following structure:

LibBase
|
______|_____
| |
| |
v v
AppBase LibChild
| |
______|_____ ____|
| | |
| | |
v v v
AppChild1 AppChild2


This is the classic "diamond" multiple inheritance problem. Unfortunately, "LibChild" is not virtually inherited from "LibBase". There's also the added complication of "AppChild1".


To clarify the behavior I want:

  • Instantiations of "AppChild1" and "AppChild2" to be treatable as instances of "AppBase"
  • Instantiations of "AppChild1" and "AppChild2" to be treatable as instances of "LibBase"
  • When constructed, "AppChild1" constructs "LibBase" through "AppBase"
  • When constructed, "AppChild2" constructs "LibBase" through "LibChild"


Once again, the code in the libraries ("LibBase", "LibChild") is immutable, and no virtual inheritance is used in them.

I don't really know what to do here--I think I'm stuck. I am not a C++ expert, so I'm hoping that I'm just not seeing a solution that's there or that someone can show that it's not solvable. Any suggestions?


Thanks,
-G

[size="1"]And a Unix user said rm -rf *.* and all was null and void...|There's no place like 127.0.0.1|The Application "Programmer" has unexpectedly quit. An error of type A.M. has occurred.
[size="2"]

Advertisement

Use composition instead of inheritance?

You're in a pickle, for sure, and I feel like the best solution might be to change what you want, but it's hard to give any kind of concrete suggestions without knowing exactly what lib/app are and why you want/need things that kind of inheritance.

[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

I haven't touched multiple inheritance in a long time. My recommendation would be to avoid using multiple inheritance if you can, as it is pretty tricky, error-prone, and I would say is usually not worth the effort involved (ie an alternative design can meet your requirements just as well and will likely be easier to implement).

Does AppChild2 contain two instances of LibBase, or just one? I would think that you just want one instance of LibBase to do multiple inheritance correctly. I can't remember how the two super classes would share one instance of that base class, unfortunately. Does ordering the construction of the base classes in the AppChild2 constructor have any effect? Remember to use the access specifier for these base classes in your constructor, because if you just rely on them instantiating themselves using their no-arg constructors in the constructor body, I think that the order is not guaranteed (again, can't recall if this is the case). In other words:

AppChild2::AppChild2() : LibChild(), AppBase() { // constructor body here }

Hero of Allacrost - A free, open-source 2D RPG in development.
Latest release June, 2015 - GameDev annoucement

Does AppChild2 contain two instances of LibBase, or just one? I would think that you just want one instance of LibBase to do multiple inheritance correctly. I can't remember how the two super classes would share one instance of that base class, unfortunately. Does ordering the construction of the base classes in the AppChild2 constructor have any effect? Remember to use the access specifier for these base classes in your constructor, because if you just rely on them instantiating themselves using their no-arg constructors in the constructor body, I think that the order is not guaranteed (again, can't recall if this is the case).

Yes, there should be exactly one instance of "LibBase" for each instance of "AppChild1" and "AppChild2". All the classes have nontrivial constructors--in fact, that's one of the key reasons I need "LibChild" at all.

My understanding is that the general way you solve the diamond inheritance problem is with virtual inheritance, i.e.:


class LibBase {...};

class LibChild : public virtual LibBase {...};

class AppBase : public virtual LibBase {...};

class AppChild1 : public AppBase {...};

class AppChild2 : public AppBase, public LibChild {...};

This ensures that there is one instance of "LibBase" for each child instance. However, this particular solution is not possible since "LibChild" was defined without virtual inheritance and cannot be changed.

It sounds like generally there's not a quick fix, so, as was rightly suggested, it helps to go back to what we're originally designing.

This code is in the context of maintenance (rewrite in disguise) of an abstraction layer for wxWidgets frames. There are two classes "Frame" and "FrameMini", which encapsulate two kinds of frames. Since they share functionality, but differ significantly, they both derive from a class "FrameBase". The designers wanted to abstract the actual functionality, so they created a backing class to handle the wx backend (which I called "AppBase"). However, the way you construct one kind of frame ("wxFrame", i.e. "LibBase") is different from how you construct another ("wxMiniFrame", i.e. "LibChild"). So, two subclasses were born: "AppChild1" and "AppChild2". The inheritance is as follows (classes renamed):


wxFrame
|
______|_____
| |
FrameBase | |
| v v
____|____ Backing wxMiniFrame
| | | |
| | ______|__________ |
v v | | |
Frame FrameMini | | |
v v v
BackingFrame BackingFrameMini


In the above:

  • On construction, instances of "Frame" create a new instance of "BackingFrame" and pass it to their parent instance of "FrameBase".
  • On construction, instances of "FrameMini" create a new instance of "BackingFrameMini" and pass it to their parent instance of "FrameBase".
  • "FrameBase" stores these in a pointer of type "Backing".

The above suggests a new design, where the "Frame" and "FrameMini" classes simply instantiate "wxFrame" or "wxMiniFrame" directly. I don't see any immediate problems with this, and it's simpler, which is only good. Thanks for bouncing ideas.

[size="1"]And a Unix user said rm -rf *.* and all was null and void...|There's no place like 127.0.0.1|The Application "Programmer" has unexpectedly quit. An error of type A.M. has occurred.
[size="2"]

Is the Backing class changeable for you? Maybe cou could split it up into a functionality part which does not inherit from wxFrame and a (possibly templated) combiner class which inherits from Backing and wxFrame and another combiner class which inherits from Backing and wxMiniFrame. Then let Backingframe inherit from the first combiner class and BackingframeMini inherit from the second.
Or leave the combiner class out and have BackingFrame inherit Backing and wxFrame and BackingframeMini inherit Backing and wxMiniFrame.
The above suggests a new design, where the "Frame" and "FrameMini" classes simply instantiate "wxFrame" or "wxMiniFrame" directly. I don't see any immediate problems with this, and it's simpler, which is only good.
The immediate problem is that wxFrame seems to require a subclass to handle event processing correctly. Consequently, there needs to be a subclass.
Is the Backing class changeable for you?
Yes.
Maybe cou could split it up into a functionality part which does not inherit from wxFrame and a (possibly templated) combiner class which inherits from Backing and wxFrame and another combiner class which inherits from Backing and wxMiniFrame. Then let Backingframe inherit from the first combiner class and BackingframeMini inherit from the second.
Actually, I had tried something similar to (but not quite the same as) this, and IIRC, it was similar to how it was originally implemented. Unfortunately, the amount of functionality that must be delegated by method stubs out to the subclasses makes it inelegant in the extreme. It is particularly irksome, since many of the method calls are exactly the same between wxFrame and wxMiniFrame--so they have to be delegated out to two different methods which make exactly the same calls.
Or leave the combiner class out and have BackingFrame inherit Backing and wxFrame and BackingframeMini inherit Backing and wxMiniFrame.
This is actually essentially what I'm implementing now. Frame and FrameMini instantiate BackingFrame and BackingFrameMini. The common functionality, I'm pretty sure, lies entirely within wxFrame, so FrameBase can store it as a pointer to wxFrame. BUT, there's no need for a common backing class anymore, which fixed the virtual inheritance problem:

wxFrame
|
______|__________
| |
FrameBase | |
| | v
____|____ | wxMiniFrame
| | | |
| | | |
v v | |
Frame FrameMini | |
v v
BackingFrame BackingFrameMini


It has the disadvantage of BackingFrame and BackingFrameMini not sharing a common parent (one is kindof like an "uncle" :-)), but wrapper classes do end up being kinda messy . . .

[size="1"]And a Unix user said rm -rf *.* and all was null and void...|There's no place like 127.0.0.1|The Application "Programmer" has unexpectedly quit. An error of type A.M. has occurred.
[size="2"]

This topic is closed to new replies.

Advertisement