can I make a pointer to class member function?

I have a class called "error", that looks like this:

1
2
3
4
class error {
public:
  double memberfun(int);
} error_instance;


I want to now create a pointer to that function:

double (*myfunpointer)(int) = &error_instance.memberfun; // is this legal?
No, it's double (error::*myfunpointer)(int) = &error::memberfun;

And then, to call it, (error_instance.*myfunpointer)(argument);
http://www.newty.de/fpt/fpt.html

Edit: you won this round, hamsterman...
Last edited on
Ah right, so no way around the indirection of a class wrapper then? I'm asking primarily because I want to a) use a class because I have a group of functions I want to be able to pass to another function but b) the call to an almost trivial (float substraction) pointer function is already 8% of execution time, and I don't want to add to that by wrapping everything in a class. I guess I'll just have to do it the hard way!
If you can use STL you can look at both mem_fun and mem_fun_ref. These helper functions help to turn member function into a function pointer for ease of use.

http://www.cplusplus.com/reference/std/functional/mem_fun
http://www.cplusplus.com/reference/std/functional/mem_fun_ref
Topic archived. No new replies allowed.