CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Aug 2007
    Posts
    858

    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.

  2. #2
    Join Date
    Mar 2009
    Posts
    51

    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.

  3. #3
    Join Date
    May 2001
    Location
    Germany
    Posts
    1,158

    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.

  4. #4
    Join Date
    Aug 2007
    Posts
    858

    Re: Quick inheritance question

    Quote Originally Posted by Richard.J View Post
    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
  •  





Click Here to Expand Forum to Full Width

Featured