Sign in to follow this  

Pointers to member functions

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

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
It is possible to have function pointers / data pointers to specific class instances.
It is called Functors and Dators, and they are discussed in an article here at gamedev.net. I think it's in the enginuity series.

Share this post


Link to post
Share on other sites

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