Archived

This topic is now archived and is closed to further replies.

Vangelis

Function pointers as members of class

Recommended Posts

Vangelis    122
I have a class with a function that I'm hoping to use as a dialog callback procedure. But when I try to create a dialog using that function, I get errors like...
error C2664: 'DialogBoxParamA' : cannot convert parameter 4 from 'int (struct HWND__ *,unsigned int,unsigned int,long)'
 to 'int (__stdcall *)(struct HWND__ *,unsigned int,unsigned int,long)'  
The prototype for the function is
BOOL CALLBACK EditDlgProc(HWND,UINT,WPARAM,LPARAM);  
and I'm attempting to create the dialog (in another member function) by
DialogBox(GetModuleHandle(NULL),MAKEINTRESOURCE(IDD_EDITDLG),mainWindow,EditDlgProc);  
I'm guessing it has to do with how member functions are treated differently that non-member functions and this all involves stdcall. Is there a standard way of getting around this? [edited by - Vangelis on August 12, 2003 11:37:39 PM]

Share this post


Link to post
Share on other sites
Dobbs    164
Make the function static, then find a way to pass instance-specific information to it (for example, create a mapping from HWND -> dialog class instance).

Share this post


Link to post
Share on other sites
invective    118
The dreaded pointer to member function question:

http://www.parashift.com/c++-faq-lite/pointers-to-members.html

basically non static member fucntions have an implicit this parameter, so no matter what you do, the function parameter list is not going to match. You have to use a static function.

Share this post


Link to post
Share on other sites
Choff    122
Why not just pass the dialog, a pointer or reference to the instance you wish to perfrom the call back on?

this is probably pretty safe if the dialog is modal.

Share this post


Link to post
Share on other sites