member function pointers and inheritance

前端 未结 3 1201
南方客
南方客 2021-01-14 04:51

So i\'m working on a simple win32 wrapper for my own convenience, and i\'ve run into a slightly complicated problem.

this has alot of other members, but i\'m omittin

3条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-14 05:16

    You should read up on the Curiously Recurring Template Pattern (http://en.wikipedia.org/wiki/Curiously_recurring_template_pattern).

    Basically, you turn your base class into a template with a template parameter that specifies the sub-class.

    Perhaps it'll be easier to understand if you see it:

    namespace Windows
    {
         template 
         class AbstractWindow
         {
         public:
              void InstallHandler(UINT msgName, void (T::*)(HWND, UINT, WPARAM, LPARAM));
    
         private:
              std::map HandlerIndex;
    
         };
    }
    
    class BasicWindow : public Windows::AbstractWindow< BasicWindow >
    {
    public:
        BasicWindow() 
        {
             InstallHandler(WM_CREATE, &BasicWindow::Create);
        }
    
    private:
        void Create(HWND, UINT, WPARAM, LPARAM) {}
    };
    

提交回复
热议问题