Function Pointers!?
Well, me again.
I want to use a queue in my game and need to know how to use
function pointers!
I know that you delare a function pointer like that:
typedef int (*VPF)(int,int);
But this is only a pointer on functions like
int foo(int a,int b);
It would be great if there is a possibility to have a pointer on any function like:
int foo(int a, int b);
void bah();
FPointer f;
f = bah;
f();
f = foo;
f(12,23);
How is this done?
Thx skrwX
I doubt it''s possible, simply because functions, when defined, have a specific address space partially dependent on the number and types of parameters the function is expecting... hence when you create a function pointer to it, the function pointer expects an *exact* match of types and number of params.
You *could* try using a va_array to parse through ''...'' parameters, but I don''t know if this would work. Example:
int (*FunctionPointer)(...);
int Func1(...);
int Func2(...);
FunctionPointer = Func1;
FunctionPointer(dwNumber, szString);
FunctionPointer = Func2;
FunctionPointer(fX, fY, fZ);
Might this work? You''ll have to look up vector array parsing, which isn''t too hard once you have the concept down. Let me know if this works!
MatrixCubed
http://MatrixCubed.org
![](http://24.114.12.207/paleribbon.jpg)
You *could* try using a va_array to parse through ''...'' parameters, but I don''t know if this would work. Example:
int (*FunctionPointer)(...);
int Func1(...);
int Func2(...);
FunctionPointer = Func1;
FunctionPointer(dwNumber, szString);
FunctionPointer = Func2;
FunctionPointer(fX, fY, fZ);
Might this work? You''ll have to look up vector array parsing, which isn''t too hard once you have the concept down. Let me know if this works!
MatrixCubed
http://MatrixCubed.org
![](http://24.114.12.207/paleribbon.jpg)
[ Odyssey Project ]
June 06, 2001 05:57 AM
That can be hard to do in C++ due to it''s strict typeing. In most C compilers you could just define:
which would allow it to point to any function and pass any number of parameters to it.
In C++ more creativity is nessicary, anything short of manipualting the stack yourself probably won''t work. Or perhaps odd use of operator overloading on a fuction pointer type.
long (*FP) ();
which would allow it to point to any function and pass any number of parameters to it.
In C++ more creativity is nessicary, anything short of manipualting the stack yourself probably won''t work. Or perhaps odd use of operator overloading on a fuction pointer type.
Wrap classes around your functions.
Define the class with all the parameters your function needs as members, initialise them in the constructor, overload operator() to act as the function call.
I can''t get to my C++ Users journals right now to give you an exact reference, but there was an article recently about using this method to implement callback functions with variable paramaters in a typesafe way. Might have been the March 2001 issue?
It''s worth looking into, it beats naked function pointers out of the field. The STL uses function objects extensively.
--
Define the class with all the parameters your function needs as members, initialise them in the constructor, overload operator() to act as the function call.
I can''t get to my C++ Users journals right now to give you an exact reference, but there was an article recently about using this method to implement callback functions with variable paramaters in a typesafe way. Might have been the March 2001 issue?
It''s worth looking into, it beats naked function pointers out of the field. The STL uses function objects extensively.
--
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement