Quick inheritance question
Say that you have a event handler interface like
Code:
class KeyboardEventHandler
{
public:
virtual bool KeyDown(const KeyboardEvent&) = 0;
virtual bool KeyUp(const KeyboardEvent&) = 0;
};
and implement it something like
Code:
class ApplicationState
: private KeyboardEventHandler
{
private:
virtual bool KeyDown(const KeyboardEvent&) { return true; }
virtual bool KeyUp(const KeyboardEvent&) { return true; }
};
With possible later usage looking like
Code:
Keyboard kb;
ApplicationState appState;
kb.RegisterEventHandler(&appState);
Is the private inheritance kosher in this case? I want the ApplicationState to behave as-a KB event handler to recieve input, but I don't want the whole world to see the event handler functions - only the Keyboard class that will be sending the events. This compiles and runs fine with MSVC9, I just want to double check that it isn't something being enabled by an MSVC extension.
Re: Quick inheritance question
There's really not enough info here for me to answer the question.
In which function does the following take place?
Code:
Keyboard kb;
ApplicationState appState;
kb.RegisterEventHandler(&appState);
No code outside the ApplicationState class should be able to see the private base class.
Re: Quick inheritance question
I tend to use private inheritance for such event interfaces as well (influenced by a discussion here which I can't find at the moment), but with g++ I came into situation where a construct like yours led to an error message such as "the interface is not accessible". It work fine, however, if the event class registers itself.
Re: Quick inheritance question
Quote:
Originally Posted by
Richard.J
It work fine, however, if the event class registers itself.
You know, that must've been how I did it before. I was sitting here starting to feel kinda stupid wondering why the example code didn't work after all...