Operator
 
声明或定义一个重载运算符。

语法

{ Type | Class | Union | Enum } typename
Declare Operator Cast () As datatype
Declare Operator @ () As datatype Ptr
Declare Operator assignment_op ( [ ByRef | ByVal ] rhs As datatype )
Declare Operator [] ( index As datatype ) [ ByRef ] As datatype
Declare Operator New ( size As UInteger ) As Any Ptr
Declare Operator New[] ( size As UInteger ) As Any Ptr
Declare Operator Delete ( buf As Any Ptr )
Declare Operator Delete[] ( buf As Any Ptr )
End { Type | Class | Union | Enum }

{ Type | Class | Union } typename
Declare Operator For ()
Declare Operator For ( [ ByRef | ByVal ] stp As typename )
Declare Operator Step ()
Declare Operator Step ( [ ByRef | ByVal ] stp As typename )
Declare Operator Next ( [ ByRef | ByVal ] cond As typename ) As Integer
Declare Operator Next ( [ ByRef | ByVal ] cond As typename , [ ByRef | ByVal ] stp As typename ) As Integer
End { Type | Class | Union }

Declare Operator unary_op ( [ ByRef | ByVal ] rhs As datatype ) As datatype
Declare Operator binary_op ( [ ByRef | ByVal ] lhs As datatype, [ ByRef | ByVal ] rhs As datatype ) As datatype

Operator typename .Cast () As datatype
Operator typename .@ () As datatype Ptr
Operator typename .assignment_op ( [ ByRef | ByVal ] rhs As datatype )
Operator [] ( index As datatype ) [ ByRef ] As datatype
Operator unary_op ( [ ByRef | ByVal ] rhs As datatype ) As datatype
Operator binary_op ( [ ByRef | ByVal ] lhs As datatype, [ ByRef | ByVal ] rhs As datatype ) As datatype
Operator typename .New ( size as uinteger ) As Any Ptr
Operator typename .New[] ( size As UInteger ) As Any Ptr
Operator typename .Delete ( buf As Any Ptr )
Operator typename .Delete[] ( buf As Any Ptr )

参数

typename
TypeClassUnionEnum的名称。
assignment_op
let += -= *= &= /= \= mod= shl= shr= and= or= xor= imp= eqv= ^=
unary_op
- not * -> abs sgn fix frac int exp log sin asin cos acos tan atn len
binary_op
+ - * & / \ mod shl shr and or xor imp eqv ^ = <> < > <= >=

说明

诸如=+cast的内置运算符在表达式中使用时具有预定义的行为。当运算符 的至少一个参数是TypeClassEnumUnion数据类型时,这些运算符可能会重载以执行除预定义操作之外的其他操作。

运算符只是函数。运算符'+'具有Function Plus( A as DataType, B as DataType ) as DataType等功能。有关详细信息,请参阅操作符 Overloading .运算符可能被重载以接受不同的数据类型作为参数。只有Cast操作符可以重载才能返回不同的类型。

非静态运算符成员在TypeClass内声明。全球运算符 在外面宣布。所有操作符定义(过程体)必须出现在外面。

LetCast和其他赋值运算符必须在TypeClass内声明。它们被传递一个隐藏的This参数,并且具有与它们被声明的TypeClass相同的返回数据类型。

必须在TypeClassEnum之外声明一元运算符,并显式声明返回数据类型。一元运算符可以重载以返回任何有效的数据类型,操作符 -> (访问成员指针)必须返回TypeClass数据类型。

必须在TypeClassEnum之外声明二进制运算符,并显式声明返回数据类型。二进制运算符可以使用有效的数据类型重载,除了关系运算符,必??须返回Integer.

Let是指赋值运算符,如LET a=b所示。通常情况下,Let关键字被省略,-lang fb 方言中不允许。但是,Let()可以用于将UDT的字段赋值给多个变量。

有关超载For..Next语句以使用用户定义类型的更多信息,请参阅ForStepNext.

NewNew[]DeleteDelete[]运算符成员始终是静态的,即使没有明确声明(Static关键字是不必要的,但允许)。

例子

''operator1.bas

Type Vector2D
  As Single x, y

  ''返回一个包含矢量数据的字符串。
  Declare Operator Cast() As String

  ''将矢量乘以标量。
  Declare Operator *= ( ByVal rhs As Single )
End Type

''允许两个向量能够一起添加。
Declare Operator + ( ByRef lhs As Vector2D, ByRef rhs As Vector2D ) As Vector2D

''使用重载运算符abs()返回向量的模数(单个)。
Declare Operator Abs (  ByRef rhs As Vector2D ) As Single

Operator Vector2D.cast () As String
  Return "(" + Str(x) + "," + Str(y) + ")"
End Operator

Operator Vector2D.*= ( ByVal rhs As Single )
  This.x *= rhs
  This.y *= rhs
End Operator

Operator + ( ByRef lhs As Vector2D, ByRef rhs As Vector2D ) As Vector2D
  Return Type<Vector2D>( lhs.x + rhs.x, lhs.y + rhs.y )
End Operator

Operator Abs ( ByRef rhs As Vector2D ) As Single
  Return Sqr( rhs.x * rhs.x + rhs.y * rhs.y )
End Operator

Dim a As Vector2D = Type<Vector2D>( 1.2, 3.4 )
Dim b As Vector2D = Type<Vector2D>( 8.9, 6.7 )
Dim c As Vector2D = Type<Vector2D>( 4.3, 5.6 )

Print "a ="; a, "ABS(A)="; Abs( a )
Print "b ="; b, "ABS(B)="; Abs( b )
Print "a + b ="; a + b, "abs(a + b)="; Abs( a + b )
Print "c ="; c, "ABS(C)="; Abs( c )
Print "'c *= 3'"
c *= 3
Print "c ="; c, "ABS(C)="; Abs( c )

对齐内存赋值器:
    • 通过使用重载的成员运算符“新建”和“删除”,任何创建的用户对象与“ALIGN”字节的倍数(本例中为256字节)对齐,
    • 赋值的存储器的真正指针保存在用户指针的上方,位于填充块中。
''operator2.bas

Const ALIGN = 256

Type UDT
  Dim As Byte a(0 To 10 * 1024 * 1024 - 1) ''10兆字节固定数组
  Declare Operator New (ByVal size As UInteger) As Any Ptr
  Declare Operator Delete (ByVal buffer As Any Ptr)
  Declare Constructor ()
  Declare Destructor ()
End Type

Operator UDT.New (ByVal size As UInteger) As Any Ptr
  Print "重载新操作符,参数size = &h" & Hex(size)
  Dim pOrig As Any Ptr = CAllocate(ALIGN-1 + SizeOf(UDT Ptr) + size)
  Dim pMin As Any Ptr = pOrig + SizeOf(UDT Ptr) 
  Dim p As Any Ptr = pMin + ALIGN-1 - (CULng(pMin + ALIGN-1) Mod ALIGN)
  Cast(Any Ptr Ptr, p)[-1] = pOrig
  Operator = p
  Print "真正的指针= &h" & Hex(pOrig), "返回指针= &h" & Hex(p)
End Operator

Operator UDT.Delete (ByVal buffer As Any Ptr)
  Print "重载删除操作符,参数buffer = &h" & Hex(buffer)
  Dim pOrig As Any Ptr = Cast(Any Ptr Ptr, buffer)[-1]
  Deallocate(pOrig)
  Print "真正的指针= &h" & Hex(pOrig)
End Operator

Constructor UDT ()
  Print "构造函数,@This = &h" & Hex(@This)
End Constructor

Destructor UDT ()
  Print "析构函数,@This = &h" & Hex(@This)
End Destructor

Print "'Dim as UDT Ptr p = New UDT'"
Dim As UDT Ptr p = New UDT

Print "p = &h" & Hex(p)

Print "'删除p'"
Delete p

输出示例:
'Dim As UDT Ptr p = New UDT'
  Overloaded New operator, with parameter size = &hA00000
  real pointer = &h420020   return pointer = &h420100
  Constructor, @This = &h420100
  p = &h420100
'Delete p'
  Destructor, @This = &h420100
  Overloaded Delete operator, with parameter buffer = &h420100
  real pointer = &h420020
操作符“[]”的小型用例:字节缓冲区的最简单的智能指针。
''operator3.bas

''智能指针是一个像指针一样的对象,但不仅仅是一个指针:
'' - 该对象作为指针是灵活的,并且具有作为对象的优点,
''像构造函数和析构函数自动调用。
'' - 因此,智能指针的析构函数将被自动调用
''当该对象超出范围时,它将删除用户指针。

''字节缓冲区最简单的智能指针示例:
'' - 构造函数和析构函数允许赋值,释放和调整字节缓冲区的大小。
'' - 指针索引运算符允许访问缓冲区元素。
'' - 复制构造函数和let-operator仅在私有部分声明,
''以便禁止复制构造和任何作业。

Type smartByteBuffer
  Public:
    Declare Constructor (ByVal size As UInteger = 0)
    Declare Operator [] (ByVal index As UInteger) ByRef As Byte
    Declare Destructor ()
  Private:
    Declare Constructor (ByRef rhs As smartByteBuffer)
    Declare Operator Let (ByRef rhs As smartByteBuffer)
    Dim As Byte Ptr psbb
End Type

Constructor smartByteBuffer (ByVal size As UInteger = 0)
  This.destructor()
  If size > 0 Then
    This.psbb = New Byte[size]
    Print "赋值字节缓冲区"
  End If
End Constructor

Operator smartByteBuffer.[] (ByVal index As UInteger) ByRef As Byte
  Return This.psbb[index]
End Operator

Destructor smartByteBuffer ()
  If This.psbb > 0 Then
    Delete[] This.psbb
    This.psbb = 0
    Print "字节缓冲区取消赋值"
  End If
End Destructor

Scope
  Dim As smartByteBuffer sbb = smartByteBuffer(256)
  For I As Integer = 0 To 255
    sbb[I] = I - 128
  Next I
  Print
  For I As Integer = 0 To 255
    Print Using "#####"; sbb[I];
  Next I
  Print
End Scope

方言差异

参考