views:

22

answers:

1

We have this situation:


                    Window  Keyboard
                    ^         ^
                    |        /
                    ApplicationWindow 

so

class Window { }
class Keyboard { }
class AppWindow : public Window, public Keyboard { }

Now, Keyboard wants to access a property in ApplicationWindow, for example, it wants to call the ApplicationWindow's getWidth() property.

The solution I have is to place a pointer to an ApplicationWindow inside Keyboard.

I'm trying to use multiple inheritance here as composition, while I know actual composition is more strongly encouraged, I wanted to try MI.

In any case I'd need a back link to the ApplicationWindow instance from Keyboard if I used composition anyway..

+1  A: 

I would say it would be a lot cleaner to move the property into the base class Keyboard.

ApplicationWindow would have access to it as well since ApplicationWindow extends Keyboard.

You don't want the base class to need to have any knowledge of any sub classes.

Neal Donnan
I thought about it, and with this particular problem it can't be done.. :) That's why I included the MI in the question
bobobobo
OK. I guess what you're saying is, _Keyboard_ simply shouldn't be able to know the width of the _ApplicationWindow_. If it does then there's something sorely wrong with the design.
bobobobo
yeah, sounds odd for a keyboard to need to know about the screen it's connected to :). Maybe have have a screen resized event that sets the keyboard width or something if the keyboard needs a width
Neal Donnan
OK but would it be wrong then to give Keyboard cached copies of screen width and screen height? How's that sound?
bobobobo
sounds ok to give the initial keyboard values based on the initial window size if it's from a cache. I think you are right, composition would be better also.
Neal Donnan