声明或定义一个重载运算符。
语法
参数
typename
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 ^ = <> < > <= >=
说明
例子
''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
方言差异
参考