|
-
April 24th, 2009, 07:17 AM
#1
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.
-
April 24th, 2009, 08:36 AM
#2
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.
-
April 24th, 2009, 11:40 AM
#3
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.
-
April 24th, 2009, 01:12 PM
#4
Re: Quick inheritance question
 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...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|