Sign in to follow this  

Generalized member function pointers

This topic is 4596 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

Okay, this may be a bit out there, so stay with me: I'm working on a GUI class for my engine at the moment, and I've decided that the best way to handle input is through function pointers, much the same way windows does. In essence, there is a function pointer within the class called "guiProc", defined as such: void (*guiProc) ( uint, cString ); During the gui's update function it scans to see which actions (Mouse up/Mouse down/click/key press/etc.) has been preformed on which elements, then passes them through to this function with the enumerated action and the name of the element. eg: guiProc( GUI_MOUSEDOWN, "Button3" ); Elsewhere in the code we have the function that it's pointing to which dispatches these commands, usually with a big honkin' switch. I really don't think an example is nessicary here. In any case, here's my dilemma: This works great if I'm pointing the funtion pointer at an independant function. That is, one that's not associated with any particular class. However, if I try and point it to a member function of a different class, it pukes on me with a bunch of errors. In particular, I have a "cGame" class that handles all game logic. I would like to point this gui function to a function within that class so it has access to all the appropriate member variables. This is easily achived by changing the definition of my gui's function pointer to: void (cGame::*guiProc) ( uint, cString ); HOWEVER, this restricts the function to ONLY working with that particular class. That's OK for now, but I always like to design with forward compatibility in mind, and I don't like the idea of two mostly unrelated classes being tied so closely together. So, here's the question: Is there a way to generalize the function pointer in such a way that it can point to a member function of any class? I've heard that this can be a compiler specific issue, so for reference I'm using Visual C++ .NET. Thank you for any advice! EDIT: Oh, yeah, in case it helps, the error it gives me when attempting to assign it to a member function is - error C2440: '=' : cannot convert from 'void (__thiscall cGame::* )(uint,cString)' to 'void (__cdecl *)(uint,cString)'

Share this post


Link to post
Share on other sites
Yes. For a guaranteed portable way, check out boost::function; for a faster, but potentially compiler-specific way, Don Clugston's Fast Delegates. Both solutions also generalize to free functions, static member functions and function objects.

EDIT:
Quote:
error C2440: '=' : cannot convert from 'void (__thiscall cGame::* )(uint,cString)' to 'void (__cdecl *)(uint,cString)'

FWIW, member function pointers just won't fit into a regular function pointer with the same argument list; first, member function pointers contain additional data to resolve polymorphic calls, and second, they always need an object for which to call them - basically, they have an additional "this" parameter in their parameter list. boost::function allows you to either bind the object at creation time, or defer it to call time (in which case you just supply the object as one of the arguments).

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
You may also be able to get another idea from the method I used in the following article:

http://www.codeproject.com/library/DWinLib.asp

David

Share this post


Link to post
Share on other sites

This topic is 4596 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this