Skip to main content
gamedev.net
🔒 Locked

Function pointer to non static class member?

Started by Toolmaker 8 replies 450+ views
Original Post
Toolmaker
Toolmaker
I am looking for a way to make a function pointer to a non static class member. The reason is that I am making a Win32 wrapper, which takes function pointers for the called events. Inside the WndProc I search if the event has a function pointer associated, and if so, I'll call the function. The eventhandler looks like
typedef long (* MessageHandler) (wgBaseWindow *, HWND hWnd, WPARAM wParam, LPARAM lParam);
  
and to handle the events in code, I want to have something like this:
class CApp
{
public:
    long OnCreate(wgBaseWindow *Wnd, HWND hwnd, WPARAM wParam, LPARAM lParam);
    long onPaint(wgBaseWindow *Wnd, HWND hwnd, WPARAM wParam, LPARAM lParam);
};

// Inside application

CApp App;
Wnd->RegisterEvent(WM_CREATE, App::OnCreate);
Wnd->RegisterEvent(WM_PAINT,  App::OnPaint);
Is this possible? I heard it should be possible with boost::bind, but I don't understand shit of it. Anyone? Toolmaker
-Earth is 98% full. Please delete anybody you can. [edited by - toolmaker on November 24, 2003 6:29:53 AM]
 
__fold
__fold
I was just about to ask the same question.

...doesn't seem to work with static access, but it compiles.

[edited by - __fold on November 24, 2003 6:56:04 AM]
Toolmaker
Toolmaker
That doesn''t quite work with what I have. I tried creating a wgEventClass and make the function pointer to wgEventClas::*EventHandler

but I am getting alot of errors on that one. What is wrong with this?


// Event class

class wgEventClass
{
public:
wgEventClass() {}
virtual ~wgEventClass() {}
};

// Typedef of event handler

typedef long (wgEventClass::* MessageHandler) (wgBaseWindow *, HWND hWnd, WPARAM wParam, LPARAM lParam);

// App class

class CSimpleStore : public wgEventClass
{
public:
CSimpleStore();
~CSimpleStore();
void Run(HINSTANCE hInstance, LPCSTR lpCmdLine);

private:
// Events

long OnClose(wgBaseWindow *Wnd, HWND hWnd, WPARAM wParam, LPARAM lParam);
long OnSettings(wgBaseWindow *Wnd, HWND hWnd, WPARAM wParam, LPARAM lParam);

BOOL CreateMenu();

wgMDIWindow *m_Wnd;
wgMenu m_Menu;
};

// Create code inside class CSimpleStore

m_Wnd->RegisterEvent(WM_CLOSE, &CSimpleStore::OnClose);


I am not sure what I am doing wrong here.

Toolmaker



-Earth is 98% full. Please delete anybody you can.
 
Neophyte
Neophyte
Might make it easier for us if you told us what the actual errors you''re getting *are*.

In any case, this code should work

void (MyClass::*pt2Member)(void);

class MyClass {
public:
void testFunc() {
cout << "MyClass::testFunc" << endl;
}
};

pt2Member = MyClass::testFunc;

MyClass instance;
(instance.*pt2Member)();


Note the clumsy syntax for calling the method.

Personally I go with a functor rather than a function-pointer when I need member-methods.

Note, by the way, that a pointer to a

void function (void);

free function is not compatible with a pointer to a

void function (void);

method function.

- Neophyte
kuphryn
kuphryn
Interesting.

I do not see how it is possible to have a function pointer to a non-static member function. One reason is which object is the pointer pointing to?

Kuphryn
Shannon Barber
Shannon Barber
Rather than writing your own wrapper, have a look at wxWindows. It was designed with Win32 in mind, and has been ported to other environments (GTK, Motif, Photon, etc...).

...
you need a 'this' pointer to call methods (member functions) - you have to use a member function pointer as opposed to a normal function pointer.

You may want to use either Loki::Functor or Boost::function - they provide a generic function wrapper which can make this easier. (Generic functors will be added to C++0x)

...
Loki Example

#include <Loki/Functor.h>

struct wgBaseWindow
{
HRESULT OnCreate(HWND hwnd, WPARAM, LPARAM)
{
return MessageBox(hwnd, "Test", "Test", MB_OK);
}
};

int main(int argc, char* argv[])
{
typedef Loki::Functor<HRESULT, Loki::TL::MakeTypelist<HWND, WPARAM, LPARAM>::Result> wgMessageHandler;
wgBaseWindow test;
wgMessageHandler onCreate(&test, &wgBaseWindow::OnCreate);
// HWND, L, W

onCreate(NULL, 0, 0);
}



However, OnCreate is invoked /prior/ to CreateWindow returning.

- Magmai Kai Holmlor

Not For Rent

[Look for information | GDNet Start Here | GDNet Search Tool | GDNet FAQ | MSDN RTF[L] | SGI STL Docs | STFW | Asking Smart Questions ]
[Free C++ Libraries | Boost | ACE | Loki | MTL | Blitz++ | wxWindows| Spirit(xBNF)]
[Free C Libraries | zlib ]



[edited by - Magmai Kai Holmlor on November 24, 2003 12:41:21 PM]
The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
C-Junkie
C-Junkie
mem_fun<>()

*sighs*
Rob Loach
Rob Loach
quote:
Original post by Anonymous Poster
class myclass{
public:
int func(int);
};

typedef int (myclass::*p)(int);

p= &myclass.func();

myclass eg;

eg.*p(1);

myclass* peg =⪚
peg->*p(1);


That''s it.
Rob Loach [Website] [Projects] [

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.