Pointers to Functions
When I was learning C++ I couldn''t figure out why somebody would need a pointer to a function. The examples I read in books were all dumb and would have worked better without using pointers. So I basically decided they were a feature added just in case somebody could use them and didn''t bother learning about them too much.
However, this morning I was looking over some of my code, and I think I may have a use for them, but I''m not sure it''s appropriate. I''ve got these two member functions for a class, one with special optimizations for computers that support them and one without. Obviously, both will never be needed at the same time, so I''ve been checking for support at the start of the game and setting a flag that tells the program which function to call.
What I''m thinking is that when I figure out which function to call at the start of the program, I could set up a pointer to the appropriate function, and use the pointer whenever I need to instead of checking that flag. Would this work, or is it not a good idea to store the address in a class like that? I''d hate to find out that under certain circumstances the address of a function changes, and have the program break unexpectedly later down the road.
Thanks,
CM
Yes that would probably work but you'd be better off using #ifdef _CPU_OPTIMIZED type conditional compilation instead. For that particular case anyway.
OR - Inheritance instead of having two-seperate methods that due the same thing, have two - classes:
and that will only perform the check once like you were saying.
Regards,
Jumpster
EDIT: Changed CODE blocks to SOURCE blocks...
Edited by - Jumpster on February 2, 2001 11:04:58 PM
|
OR - Inheritance instead of having two-seperate methods that due the same thing, have two - classes:
|
and that will only perform the check once like you were saying.
Regards,
Jumpster
EDIT: Changed CODE blocks to SOURCE blocks...
Edited by - Jumpster on February 2, 2001 11:04:58 PM
Regards,JumpsterSemper Fi
I dont think the precompiler conditionals will help... essentially your basing the compilation on the macine where the exe is built and unless you are distributing the source this will not achieve the desired results.
The pointer to function ideal that you had might be a viable soloution to your problem.
BTW, are these pointers to functions of a C++ class? If so, you have to treat them diferently than straight C (IIRCC).
Game on,
Dave "Dak Lozar" Loeser
The pointer to function ideal that you had might be a viable soloution to your problem.
BTW, are these pointers to functions of a C++ class? If so, you have to treat them diferently than straight C (IIRCC).
Game on,
Dave "Dak Lozar" Loeser
Dave Dak Lozar Loeser
"Software Engineering is a race between the programmers, trying to make bigger and better fool-proof software, and the universe trying to make bigger fools. So far the Universe in winning."--anonymous
"Software Engineering is a race between the programmers, trying to make bigger and better fool-proof software, and the universe trying to make bigger fools. So far the Universe in winning."--anonymous
As far as I''m aware, using function pointers is fine in C++ so long as you keep track of whats happening.
For example, if you take a pointer (eg. int *(* myPntr)(int *tag)), and have it hold a function Blah (of the correct type), you''ll be able to use the function, provided it isnt private.
Mmm...what I mean is, if you have a private function in a class and pass the address out via say, a void * pointer, then cast that to a function pointer, and try to run it, it wont work.
But other than that, pointers behave just like in C. You certainly arent going to have the address change on you mysteriously...
For example, if you take a pointer (eg. int *(* myPntr)(int *tag)), and have it hold a function Blah (of the correct type), you''ll be able to use the function, provided it isnt private.
Mmm...what I mean is, if you have a private function in a class and pass the address out via say, a void * pointer, then cast that to a function pointer, and try to run it, it wont work.
But other than that, pointers behave just like in C. You certainly arent going to have the address change on you mysteriously...
Pointers to member functions are indeed different from pointers to global functions. I use member function pointers, this is how:
Note the use of parentheses when calling FunctionPtr; you need them. Also, despite the weird C++ code, calling FunctionPtr actually takes no longer than calling ExampleFunction directly. The assembly output looks like this:
~CGameProgrammer( );
|
Note the use of parentheses when calling FunctionPtr; you need them. Also, despite the weird C++ code, calling FunctionPtr actually takes no longer than calling ExampleFunction directly. The assembly output looks like this:
// Example.FunctionPtr = Example.ExampleFunction;mov DWORD PTR _Example$[ebp], OFFSET FLAT:?ExampleFunction@ExampleClass@@QAEEPAD@Z// (Example.*Example.FunctionPtr)("It worked!\n");push OFFSET FLAT:$SG484lea ecx, DWORD PTR _Example$[ebp]call DWORD PTR _Example$[ebp]// For comparison, assembly of calling ExampleFunction directly:push OFFSET FLAT:$SG484lea ecx, DWORD PTR _Example$[ebp]call ?ExampleFunction@ExampleClass@@QAEEPAD@Z
~CGameProgrammer( );
~CGameProgrammer( );
Developer Image Exchange -- New Features: Upload screenshots of your games (size is unlimited) and upload the game itself (up to 10MB). Free. No registration needed.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement