Skip to main content
GameDev.net gamedev.net
🔒 Locked

Pointers to member functions

Started by Kest Sep 5, 2005 at 7:25 AM 4 replies 1k views
Original Post
Kest
Kest
Is it possible to store any sort of combinations of instance pointers and/or function pointers to allow calling a member function for a specific instance? Is it possible to send, as a callback, a member function that works uniquely for one single instance of a class? IE, not a static function? Is there any way that the routines recieving the callback pointer could add functionality to get this working? If those routines don't know what type of class has sent the function, is it possible to call a function pointer associated with an instance pointer? Is there any other way to allow a callback type system to activate instance-specific class functions? Similar to messaging, perhaps? Sorry for the broad question mark. I'm trying to find a way to allow scripting functions to send data back to individual objects without knowledge of those objects. Messaging looks like my best bet. The object sends an instance pointer ('this' as void*) and a message ID for every script function, along with a pointer to a single static function to call. The script then sends a list of arguments, the void instance pointer, and the message ID back to the object's static function. The object's static function casts the void pointer back into its own instance type, then calls the member function associated with the message ID. I'm just curious to know if there is an easier way. Thanks
SiCrane
SiCrane
To make a pointer to a non-static member function of a class Foo you do:

void (Foo::* fptr)(void) = &Foo:some_function;

replace the first void with the return value and the second void with the parameter list if necessary. To call the function on a pointer to a Foo you would use:

(foo_ptr->*fptr)();

However, you may want to consider using function objects instead like boost::function with boost::bind.
Enigma
Enigma
Assuming C++ (which seems a pretty safe bet given that you mentioned void *, pointers and member functions) check out boost::bind and/or boost::signals.

Enigma
Rob Loach
Rob Loach
C# introduced a new concept of delegates and events which are pretty much a sizable array of function pointers which are all called whenever needed.
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.