Function Pointer Questions

BlitzMax Forums/BlitzMax Programming/Function Pointer Questions

Cajun17(Posted 2005) [#1]
Since this probably the only thing I have no experience with I have a few legistical question's about them. If I have a function pointer declared in an object can I:

a) have it point to a method inside the same object? I guess then it'd technically be a method pointer.

b)point to a function within the object?

b.1) point to a free frunction?
b.2)if it points to a free function does this function aquire scope within the object and see it's fields? does it become like a type function or is it simply called from the pointer just as it appears in the code as a free function?


Tiggertooth(Posted 2005) [#2]
First type (class) I wrote in BlitzMax was a state machine that used function pointers...based on that experience here's my answers to your questions:

a) No, methods aren't static functions whose address can be stored off in a function pointer.

b) Yes, a function's address is static, and therefore can be assigned to a function pointer. Works great!

b1) Yes
b2) No

My suggestion is to use functions within Types. This keeps the encapsulation of the functionality within the type itself--you don't have misc free functions floating about. But, make the first argument to the function an Object. Then, inside the function, cast the object to the Type. You can now invoke type methods on this object.

This was my solution. It is reasonably elegant and works very well. Any type in my engine can now include (or inherit from) my state machine and have full state machine functionality. Whenever the machine changes states, functions inside your type will get called (each state supports a unique enter, exit, and update function). I've been waiting a long time for function pointers in Blitz, and I'm very glad they've arrived!

Ken


Cajun17(Posted 2005) [#3]
Actually all your answers are what I expected more or less. I'll try some stuff out. With what I'm doing now I think function pointers are the only clean way to do what I want.
Thanks for the reply.