FUNCTION 指针
- 来源: https://www.freebasic.net/wiki/wikka.php?wakka=KeyPgFunctionPtr
- 最后更新: 2021-08-03
存储指向返回值的 FUNCTION 过程指针的数据类型
语法
dim variable as Function [CDecl|Pascal|StdCall] [( [parameter_list] )] [ ByRef ] [As return_type] [= initializer]参数
parameter_list:parameter[, parameter[, ...]]
parameter:[ByRef|ByVal] identifier [As type] [= default_value]
identifier:函数中引用的变量名称
type:变量的类型
default_value:调用时若未指定参数则使用的默认值
return_value:函数返回的值
initializer:用于设置初始值的函数地址
说明
Function 指针是一种过程指针,存储返回值的已编译代码的内存地址。若未给出初始化器,则默认初始值为零(0)。
可以通过 ProcPtr 或 Operator @ (Address of) 取函数的地址,并将其赋值给变量来设置 Function 过程的内存地址。
该过程必须与声明的 Function 指针具有相同的 Function 声明签名。
要调用已赋值的函数,像调用普通声明的 Function 一样使用 variable 名称,参数列表必须始终用括号括起来(即使为空)(不带括号时,只会访问指针值,即函数的地址)。
Function 指针的主要用途之一是创建回调过程:
回调
Function是通过参数(Function指针)传递给另一个过程的Function,该过程预期会在适当时机调用(执行)该"参数"。若回调
Function在调用返回给调用方代码之前完全执行完毕,则称该回调过程为"同步"的。若调用立即返回给调用方代码,且回调
Function与调用方的后续代码并行运行,则称该回调过程为"异步"的。
示例
start GeSHi
Function ConcatSelf( x As String ) As String
Return x & x
End Function
Dim x As Function( x As String ) As String = ProcPtr( ConcatSelf )
Print x( "Hello" )end GeSHi
start GeSHi
Function x2 (ByVal i As Integer) As Integer
Return i * 2
End Function
Function x3 (ByVal i As Integer) As Integer
Return i * 3
End Function
Function operation (ByVal i As Integer, ByVal op As Function (ByVal As Integer) As Integer) As Integer
Return op(i)
End Function
Print operation(4, @x2)
Print operation(4, @x3)end GeSHi
start GeSHi
' Example of basic callback Function mechanism (asynchronous) to implement a key pressed event:
' (the user callback Function address cannot be modified while the event thread is running)
' - An asynchronous thread tests the keyboard in a loop, and calls a user callback Function each time a key is pressed.
' - The callback Function address is passed to the thread.
' - The callback Function prints the character of the key pressed,
' but if the key pressed is `<escape>` it orders the thread to finish by using the function return value.
' - As the user callback address is passed to the thread as argument, it cannot be modified while the thread is running.
'' thread Sub definition
Sub threadInkey (ByVal p As Any Ptr)
If p > 0 Then '' test condition callback Function defined
Dim As Function (ByRef As String) As Integer callback = p '' convert the any ptr to a callback Function pointer
Do
Dim As String s = Inkey
If s <> "" Then '' test condition key pressed
If callback(s) Then '' test condition to finish thread
Exit Do
End If
End If
Sleep 50, 1
Loop
End If
End Sub
'' user callback Function definition
Function printInkey (ByRef s As String) As Integer
If Asc(s) = 27 Then '' test condition key pressed = `<escape>`
Print
Return -1 '' order thread to finish
Else
Print s;
Return 0 '' order thread to continue
End If
End Function
'' user main code
Dim As Any Ptr p = ThreadCreate(@threadInkey, @printInkey) '' launch the thread, passing the callback Function address
ThreadWait(p) '' wait for the thread finishend GeSHi
与 QB 的区别
- FreeBASIC 新增
另请参阅
FunctionProcPtrOperator @ (Address of)
返回 目录