Jump to content
  • Advertisement
Sign in to follow this  

Multiple inheritance do's and don'ts

This topic is 5402 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello again, A few of my recent threads have reminded me once again how much farther I have to go in learning things. As part of that learning process I've been re-writing a part of my GUI which I found to be awkward in the past, this time utilizing inheritance. Alas, I fear I've gone overboard with the inheritance :D The render objects I have are arranged in an n-tree [I think? 1 parent, multiple children] Up until now I've created 5 classes:
base - Contains parent, children, virtual render(), virtual dtor
+rect - adds rectangle coords to base
++root - Root render object, adds renderer specific info such as the DX device to rect
++text - adds a string, font info, color, and text formatting to rect
+image - adds vertex and texture info to base
So far, so good; everything works as it's supposed to. Code duplication is by far less, and a lot of the awkward areas have become much more straightworward [in exchange for other areas becoming more complex...] Now I'm looking to create a class for textinput. I was originally planning on making it inherit from both text and image. Before starting though I looked through some multiple inheritance docs [from thinking in cpp 2] which was nice enough to detail some of the headaches and logistics involved with multiple inheritance with a common ancestor. It also noted that what I'm looking to do could probably be done with virtual base functions. Doing so looks as though it might be a little messy, but workable. While it looks as though I've designed myself into a corner a little, I'd like to impliment the MI textinput class to learn a bit before making it a standalone with image and text children or starting over if need be. Before that though, I'd like to ask others for a little feedback as to what to do and what to avoid when dealing with these situations [beyond the obvious, 'avoid the situation'].

Share this post

Link to post
Share on other sites
You'll want to virtually inherit from base so that textinput only gets one copy of it, instead of one from text (via rect) and one from image.

class Base {};
class Rect : public virtual Base {};
class Text : public Rect {};

class Image : public virtual Base {};

class TextInput : public Text, public Rect {};

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!