Sign in to follow this  

Pointers to Functions within instanced classes

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

	A alpha;
	alpha.a = 10;

	Call((alpha.Func)());


Assuming Call takes a void function pointer as a parameter, does anyone know the syntax for this, with different class types?

Share this post


Link to post
Share on other sites
Your post isn't very detailed, so I'm not completely sure what you are asking. Are you trying to pass a member function as an argument in C++? If so, you can't do that. The problem is that non-static member functions work differently than regular functions, so they are not supported by C++ function pointers.

In Managed C++, this can be done with delegates.

Share this post


Link to post
Share on other sites
Quote:
Original post by nimrand
The problem is that non-static member functions work differently than regular functions, so they are not supported by C++ function pointers.


You can have method pointers in c++ to non-static class methods. They are declared like so:
  void ( classname::*mthdptr )() // no args, returns void
Using them is a little convoluted though:
classname c;
void ( classname::*mthdptr )();
mthdptr = classname::test; // assign method to pointer
( c.*mthdptr )(); // call method



You're probably better off implementing a class that stores a pointer to the class and calls the appropriate method. I don't know Boost, but jdhardy's suggestion seems to be exactly that. [edit] Err, actually Boost's website mentions argument binding, and gives several possibilities here.

Regards,
jflanglois

[edit] Genjix, for what you are trying to do you may want to look at the section on callbacks at function-pointer.org

Share this post


Link to post
Share on other sites

Ok, thanks.


i cant get it to work with different object types though :/, how can i do this?


the reason i ask is that the current method i use involves inheriting an abstract base class, but since all this base class consists of is one function and 3 overloaded functions, i want to cut it out and replace with a function with 3 function pointer arguments... :D


Thanks again.

Share this post


Link to post
Share on other sites
Quote:
Original post by Genjix

Ok, thanks.


i cant get it to work with different object types though :/, how can i do this?


the reason i ask is that the current method i use involves inheriting an abstract base class, but since all this base class consists of is one function and 3 overloaded functions, i want to cut it out and replace with a function with 3 function pointer arguments... :D


Thanks again.

I think you could do that with templates(untested):

template<class Class>
void call(void (Class::*method)(), Class object) {
(object.*method())();
}




or, an even more flexible function(again, untested):

template<class Class, typename Arguments>
void call(void (Class::*method)(Arguments), Class object, Arguments args) {
(object.*method(Arguments))(args);
}



Share this post


Link to post
Share on other sites

Thanks for the solution, didnt think oof templates :P.


jflanglois : what i WAS doing was every class that used the function, inherited a base class with the function as a member, and the functions that would be passed in here were abstracted (meant to be overriden, and any calls to them were meant to be to the parent class).


Share this post


Link to post
Share on other sites

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