Skip to content

Type(别名)


为类型声明一个替代名称

语法

Type typename as symbol

参数

typename

新的替代名称。

symbol

typename 关联的符号或数据类型声明。

说明

symbol 可以引用任何已声明的数据类型,包括内置数据类型、SubFunction 指针、Type 声明、Union 声明或 Enum 声明。

Type 别名可用于允许在过程声明中对参数进行前向声明,但只能用于指针(不管其传递方式),或者用于参数(不包括数组)但仅通过引用传递或返回。一般来说,此类过程的主体需要在代码的后面、实际类型定义完善之后才能实现(例如因为传递了这样的引用;或者当传递的这样的指针随后以任何形式被解引用时)。

Type 别名也可以用于允许在用户自定义类型中前向声明数据字段,但只能用于指针。

必须使用类型别名来允许声明指向函数指针的指针,否则 ptr 将应用于返回类型而不是函数。

Type 别名声明也可以嵌套在 TypeUnion 的 UDT 结构声明内部。

示例

start GeSHi

vb
Type ParentFwd As Parent
Type Child
    Name As ZString * 32
    ParentRef As ParentFwd Ptr
    ''...
End Type

Type Parent
    Name As ZString * 32
    ChildList(0 To 9) As Child
    ''...
End Type

Dim p As Parent
p.Name = "Foo"
With p.ChildList(0)
    .Name = "Jr."
    .ParentRef = @p
    '' ...
End With   

With p.ChildList(0)
    Print .Name; " is child of "; .parentRef->Name
End With

end GeSHi

start GeSHi

vb
Function triple (ByVal i As Integer) As Integer
    Return 3 * i
End Function

Type As Function (ByVal As Integer) As Integer function_alias

'Dim As Function (Byval As Integer) As Integer f  ''this syntax works but is less readable than the next code line
Dim As function_alias f
f = @triple
Print f(123)

'Dim As Function (Byval As Integer) As Integer Ptr pf  ''this syntax does not work because Ptr applies on Integer and not on function
Dim As function_alias Ptr pf                           ''this syntax works
pf = @f
Print (*pf)(123)  ''the dereferenced pointer to procedure pointer must be enclosed in parentheses

end GeSHi

版本

  • 自 fbc 1.10.0 起:允许在声明 UDT(Type 或 Union)的结构中嵌套 Type 别名声明。

与 QB 的区别

  • FreeBASIC 新增

另请参阅

  • Type...End Type
  • Type (Temporary)

返回 目录

基于 FreeBASIC 官方文档翻译 如有侵权请联系我们删除
FreeBASIC 是开源项目,与微软公司无隶属关系