Function pointers

BlitzMax Forums/BlitzMax Programming/Function pointers

vinians(Posted 2013) [#1]
Hi guys I have an issue. I need a pointer to a function that returns a Float. I Tryed to find in the forum but only found about pointers to functions that doesnt return nothing. How can I do that?


Kryzon(Posted 2013) [#2]
Function getFloat:Float( val:Int )
	Return Float(val + 0.123)
End Function

Local myPointer:Float( parameter:Int )	'The pointer is declared like a function
					'with the same amount and type of
					'parameters and the same return type.

myPointer = getFloat 'Without the parentheses.

Print String( myPointer( 1 ) )



vinians(Posted 2013) [#3]
Thank you! Is this possible with methods too or only with functions?


Yasha(Posted 2013) [#4]
It's technically possible in the strict sense (you need to get the pointer using reflection though), but if you want to do something using a method you probably ought to find another way, like using a delegate object. If you extract a method's function pointer, it's no longer attached to its "self" object, and thus the point of it being a method is lost; it's just another function taking an object as an extra first parameter. If you intend to take a pointer to a procedure, it makes more sense to write it out as a function in the first place and take the pointer normally.