Is it possible to create an array or list of function pointers so that not all of them accept the same type of parameters?
Like
double F1(int x, int y) {return 1.0};
double F2(double x, double y) {return 2.0};
arr[1] = &F1;
arr[2] = &F2;
...
I've tried templates, but since I can't instantiate a class defined with a template without specifying a type, I can't get a non homogeneous list of function pointers...
I thought in theory this should be possible, because the array will only store addresses. Of course, since if this array could be built you would't be able to know a priori the type of the arguments of each function, I guess the restriction is somewhat coherent.
Anyway, I need to store a list of distance functions, some of them accepting doubles, some of them accepting dates, or integers... If someone has a tip about how to manage that without tayloring the functions so that all of them accept doubles, I'd be very grateful.
struct distance_function{
union{
void* function;//any pointer can be assigned to void*
int (*f_int)(int, int);//these are here so that you don't have to cast void*
double (*f_double)(double, double); //to appropriate function to use it.
};
int type;//you need a way to know which function is stored
};
However, whatever problem you're facing, I don't think this is a good solution. What exactly are you trying to do?
I have an input file with a list of sets of several non homogeneous coordinates (angle, date, time, ...) and each of the type of coordinate has a pre-defined distance).
I have to work with a function of the distances between the coordinates of two of those sets, (like:
f(dist(date1,date2),dist(time1,time2),dist(angle1,angle2),...) and in theory the user ought to be able to alter the order of the data and to provide a function distance for each coordinate.
I'll try your approach, maybe that is what I need :)
About the array of function pointers, I was thinking something along the lines of hamsterman,
but using a class/struct with various overloads for the function call operator1.
You could then have a vector or array of this class.
1 we would be using functions rather than function pointers
EDIT: come to think of it - you wouldn't need an array of the class, just one object/instance, or maybe just an on-the-spot temprary would do.