Casting through function pointers..
Hey there..
I am working on a project where I am relying heavily on function pointers. One of the problems I am having is the ''generic'' versus ''specific'' variables. Take a look:
Generic Function
int GenericFunction(unsigned long param);
Specific Function
int DrawMouse(POINT *point);
Well they both return an int and both take, in some form, a 32 bit value. It is just that in one case it is a long, and in the other case it is a pointer.
Now, if I have a generic function pointer of the following type:
int (*GenericFunctionPointer)(unsigned long param);
I can''t do
GenericFunctionPoint = DrawMouse;
because the compiler freaks out -- the types are all different!
Well, I don''t WANT to have to have my DrawMouse be:
int DrawMouse(unsigned long param)
because then I have to explicitly cast going into and coming out of the function:
GenericFunctionPointer = DrawMouse;
GenericFunctionPointer((unsigned long)(&point));
and in the function:
int DrawMouse(unsigned long param)
{
printf("x: %d, y: %d\n", ((POINT *)(param))->x, ((POINT *)(param))->y);
}
Is there some way I can trick the compiler to just let it slide? That I want it to allow me to stick any function into a function pointer and leave it up to me to make sure that the data is correct going in and out? I can''t figure out how MFC does this with the ON_COMMAND() macros, but my application is similiar. I don''t want to have to cast all kinds of data left right and center..
Any ideas?
}
typedef int (*GenericPointer)(unsigned long param);
GenericPointer GenericFunctionPointer = (GenericPointer)DrawMouse;
i have rarely used typedefs for function pointers, but this is the general idea. fiddle with the syntax until you get it right.
GenericPointer GenericFunctionPointer = (GenericPointer)DrawMouse;
i have rarely used typedefs for function pointers, but this is the general idea. fiddle with the syntax until you get it right.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement