这一章我们来讲一下VBS中的函数指针,我们都知道函数代码是程序算法的一部分,它和数组一样也需要占用一部分的存储空间,也都有相应的地址。我们不但可以可以使用指针变量指向数组的首地址,同样也可以使用指针指向函数代码的首地址,我们把指向函数代码首地址的指针变量称为函数指针。
GetRef ---- 它所提供的功能被称为函数指针,即它指向了在指定事件发生时要执行的过程的地址。
实例
'变量强制声明
Option Explicit
'定义变量
Dim new_helloworld
'定义 helloworld 函数
Function helloworld(content)
MsgBox content
End Function
'使变量 new_helloworld 指向 helloworld 函数
Set new_helloworld = GetRef("helloworld")
new_helloworld "tester"
这段代码的意思就是把变量new_helloworld的指针指向helloworld函数,这样new_helloworld就具有了helloworld函数的功能。