Archived

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

pointer to class member func question

This topic is 5117 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I''ve just decided to port a C style project of mine to C++ to tidy it up, and make it a little easier to extend. For no good reason I''m a fairly anti OOP person, but I can see it does have positives. One of them however isn''t the ability to obtain pointers to a classes member function very easily. At the moment I have a callback procedure "out in the open" so to speak - I would like it to be contained in a class. A little "diagram" : I have a class type CApplication. There is one (global) instance of it called Application. It has a member function called navPaneDlgProc(...) When I try to use it as the callback proc in : CreateDialog (hThisInstance, "navPaneDlg", hwnd, (DLGPROC)&Application.navPaneDlgProc); Compiler barfs saying
quote:
ISO C++ forbids taking the address of a bound member function to form a pointer to member function. Say `&CApplication::navPaneDlgProc''
If I do as it says, and use CreateDialog (hThisInstance, "navPaneDlg", hwnd, (DLGPROC)&CApplication::navPaneDlgProc); the compiler still complains :
quote:
converting from `bool (CApplication::*)(HWND__*, unsigned int, unsigned int, long int)'' to `BOOL (*)(HWND__*, unsigned int, unsigned int, long int)''
No amount of type casting appears to solve the problem - any ideas? I cannot accept that it is impossible to use a class member function in this way - I''m just not quite sure how you go about it

Share this post


Link to post
Share on other sites
If you have a pointer to a member function, you need to know what object it''s being called on. It''s just like calling a non-static method. So, you can''t directly pass a pointer to a member function into a Windows callback. What you can do, however, is to make your main ''callback'' function a normal (non-static) member, and have another static class member which you can pass into the windows API as a callback. Most of the Win32 functions that need a callback allow you to specify a ''user data'' parameter that then gets passed to the callback function. So, pass the class''s this pointer as the user data argument, and have your static callback take that pointer, and then call the ''real'' function on that object.

Share this post


Link to post
Share on other sites