I am trying to make a vector hold void pointers to functions, which will later be called secuentially.
So, lets say that I have got three functions. int a(), void b();, bool c();
My vector is vector<void *> vec;
And my function that stores pointers to functions.
void registerFunction(void *func)
{
vec.push_back(func);
}
Now my problem is when trying to call all the functions stored, since they are all void pointers, I just cannot call the functions without knowing their type.
So my question is… is there any way to store types of symbols so I can relate them to their respective pointers and then typecast when calling a void pointer to a function?
Note: Functions won’t be always be of type, for example, void (*)(), I will want to add methods also, hence ie. void (someclass::)(). Is it asking for too much? Should it work?
void*can’t safely be used as a generic pointer-to-function type (though you might be able to get away with it).But any function-to-pointer value can be converted to another pointer-to-function type and back again without loss of information. So you can use, for example,
void (*)()(pointer to function returning void and taking no arguments) as a generic pointer-to-function type.As for storing the type, I’m not sure that’s possible. If there are only a limited number of possibilities, you can use an enumeration type and a switch statement.
But you’ll probably be better off using a design based on inheritance.