Hello,
(My question contains a trace amount of the Win32 API, so if any of you wizards see what I'm trying to do, and know a better way of doing it, please say so).
I'm trying to make a mini-wrapper for the Win32 API - "mini" because this is for some project I want to give a maker over, and because it'll only wrap windows (poorly).
Through my frustration, I've narrowed my problem down to the example below:
foo.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
#ifndef _FOO_H_
#define _FOO_H_
#include <windows.h>
class Base {
public :
Base() {}
void init();
LRESULT (CALLBACK Base::*fptr)(HWND, UINT, WPARAM, LPARAM);
virtual LRESULT CALLBACK WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) = 0;
};
class Derived : public Base {
public :
Derived() {fptr = &Derived::WndProc; init();}
LRESULT CALLBACK WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {}
};
#endif
|
foo.cpp
1 2 3 4 5 6 7 8 9 10 11
|
#include "foo.h"
void Base::init() {
WNDCLASS wc;
//do things with wc...
wc.lpfnWndProc = (WNDPROC)*fptr;
//do other things...
}
|
Note, that foo.h has changed a lot, due to my trying different potential solutions.
Basically, Base has a pure virtual window procedure callback function "WndProc".
It also has a non-static member function pointer "fptr", which is not initialized.
Derived has a temporary body for the WndProc callback, which does nothing.
The idea is that Base represents a generic window class, and Derived is just one possible window class, as there could be any number of other window classes inheriting from the generic one (each one needs it's own WndProc).
However, each derived class needs to be able to assign fptr its own implementation of the WndProc callback, before Base::init() can use it.
Originally, I tried to do this with all kinds of different solutions, all of which are illegal apparently.
At this point, my brain can't even visualize the timeline of my stress properly, but it looked something like this:
1.) Passing default member function pointers to the Base ctor (before init() was conceived)
2.) Weird initializer lists - Something about how you can't initialize base members in derived classes, and how the Derived class wouldn't know what fptr was.
3.) std::tr1::function / bind / lambda / whatever
4.) Killing myself
That's it. I know I'm missing a lot of important details, but I'm really burnt out from this.
The real question is more of a request, which is : if you know a better way of doing this, halpppp.