Object Oriented Function Pointers?

BlitzMax Forums/BlitzMax Beginners Area/Object Oriented Function Pointers?

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

There is currently no such thing as a method pointer and no way to access it.

To create a function pointer you define it as a dummy function:

Local myfunctionpointer()

Then you can treat it like a variable, although it mainly only can interact with other function pointers. All functions that you actually create in your program have a function pointer - which is the name of the function. For example:

Function myfunction()
print "yes, by Jesus!"
End Function

There is now a function pointer in existence called myfunction.

So you can create a dummy function pointer and then make it point to any other existing function at your leisure, for example:

Local myfunctionpointer()

Function myfirstfunction()
print "one is where it all starts!"
End Function

Function mysecondfunction()
print "two makes for a good game!"
End Function

myfunctionpointer=myfirstfunction 'WITHOUT THE ()!
myfunctionpointer() 'CALLS THE `FIRST` FUNCTION!

myfunctionpointer=mysecondfunction 'WITHOUT THE ()!
myfunctionpointer() 'CALLS THE `SECOND` FUNCTION!

myfunctionpointer=myfirstfunction 'WITHOUT THE ()!
myfunctionpointer() 'CALLS THE FIRST ONE AGAIN.

You can I think change a function pointer into a byte pointer with Byte Ptr(myfunctionpointer), but that is the only translation allowed. If you want an Int pointer it has to be Int Ptr(Byte Ptr(myfunctionpointer)).

To assign a function to a function pointer, refer to the function you want to use without the `()` on the end, otherwise the function would actually be called.

To call the function assigned to your function pointer, just use your function pointer as if it's calling that function ie myfunctionpointer().

You can set up parameters for your functions. If you create a function that you want to use and it has an Int and a Float as the parameters, you also must define your function pointer variable with the same parameters. They must map onto the same params and variable types! e.g.

Function actualfunction(Param1:Int,Param2:Float)
Print "doing stuff"
End Function

Local myfunctionpointer(Thing:Int,Thing2:Float)
myfunctionpointer=actualfunction
myfunctionpointer(15, 25.934)

You can get the function pointer of a function that is defined within a custom Type, but you can't get the function pointer of a Method. Also you will notice if you manage to print out the function pointer, that there is only one instance of a `function within a type` that is shared amongst all instances of that type. The memory address is the same as used by all instances, even though it appears that each instance has its own function.

If you get some problem whereby it seems like the function is actually called rather than just returning the function pointer, make sure you don't use the () on the end and make sure to simplify the operation on its own line with no other fancy combinations of operations at once. Just do myfunctionpointer=realfunctionpointer on a line by itself, so as not to confuse things.

You can create an instance of a type at runtime, and then equate the function pointer of one of its functions to your own function pointer variable declared elsewhere. However, again, all instances of that type share the same actual function, so there is no way to create custom functions at runtime. Believe my I tried! ;-)

I hope this helps. I think that about covers it.