Advertisement

Casting through function pointers..

Started by February 04, 2000 05:13 PM
1 comment, last by Sphet 24 years, 10 months ago
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.
Advertisement
Hey foo,

Yeah I hadn''t really thought of making a new type, but of course that''s what they are there for. It sure makes it more legible. Now on to other problems with callback owners and context within classes. Goofy.

This topic is closed to new replies.

Advertisement