Operator @(取地址)
- 来源: https://www.freebasic.net/wiki/wikka.php?wakka=KeyPgOpAt
- 最后更新: 2019-07-04
返回字符串字面量、变量、对象或过程的地址。
语法
declare operator @ ( byref rhs as T ) as T pointer用法
result = @ rhs参数
rhs
要获取地址的字符串字面量、变量、对象或过程。
T
返回值
返回右侧操作数(rhs)的地址。
描述
Operator @(取地址)返回其操作数的内存地址。
当操作数是 String 类型时,返回内部字符串描述符的地址。使用 Operator Strptr(字符串指针)来获取字符串数据的地址。
操作数不能是数组,但可以是数组元素。例如,"@myarray(0)" 返回 "myarray(0)" 的地址。
此运算符可以使用适当的语法作为成员 Operator 为用户定义类型重载。
示例
start GeSHi
vb
'This program demonstrates the use of the @ operator.
Dim a As Integer
Dim b As Integer
Dim addr As Integer Ptr
a = 5 'Here we place the values 5 and 10 into a and b, respectively.
b = 10
'Here, we print the value of the variables, then where in memory they are stored.
Print "The value in A is ";a;" but the pointer to a is ";@a
Print "The value in B is ";b;" but the pointer to b is ";@b
'Now, we will take the integer ptr above, and use @ to place a value into it.
'Note that the * will check the value in the ptr, just as @ checked the ptr
'for a normal variable.
addr = @a
Print "The pointer addr is now pointing at the memory address to a, value: ";*addr
addr = @b
Print "The pointer addr is now pointing at the memory address to b, value: ";*addrend GeSHi
start GeSHi
vb
'This program demonstrates how the @ symbol can be used
'to create pointers to subroutines.
Declare Sub mySubroutine ()
Dim say_Hello As Sub()
say_Hello = @mySubroutine 'We tell say_Hello to point to mySubroutine.
'The sub() datatype acts as a pointer here.
say_Hello() 'Now we can run say_Hello just like mySubroutine.
Sub mySubroutine
Print "hi"
End Subend GeSHi
方言差异
- 在 -lang qb 方言中,此运算符不能被重载。
与 QB 的差异
- FreeBASIC 新增功能
参见
VarptrProcptrOperator * (Value of)- 指针
返回 目录