• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Geometrian

A Tricky C++ Multiple Inheritance Problem

5 posts in this topic

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

Edited by Geometrian
0

Share this post


Link to post
Share on other sites

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:

 

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

1

Share this post


Link to post
Share on other sites
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.

Edited by Geometrian
2

Share this post


Link to post
Share on other sites
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. Edited by wintertime
0

Share this post


Link to post
Share on other sites
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 . . . Edited by Geometrian
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0