like: vector<void *(*func)(void *)>
...
views:
21answers:
1
A:
You can declare a vector of pointers to functions taking a single void *
argument and returning void *
like this:
#include <vector>
std::vector<void *(*)(void *)> v;
If you want to store pointers to fuctions with varying prototypes, it becomes more difficult/dangerous. Then you must cast the functions to the right type when adding them to the vector and cast them back to the original prototype when calling. Just an example how ugly this gets:
#include <vector>
int mult(int a) { return 2*a; }
int main()
{
int b;
std::vector<void *(*)(void *)> v;
v.push_back((void *(*)(void *))mult);
b = ((int (*)(int)) v[0])(2); // The value of b is 2.
return 0;
}
You can use typedef
's to partially hide the function casting syntax, but there is still the danger of calling a function as the wrong type, leading to crashes or other undefined behaviour. So don't do this.
schot
2010-09-21 07:48:52