OPERATOR
- 来源: https://www.freebasic.net/wiki/wikka.php?wakka=KeyPgOperator
- 最后更新: 2021-11-18
声明或定义重载运算符。
语法
{ Type | Class | Union } typename
declare Operator cast () [ byref ] as datatype
declare Operator @ () [ byref ] 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 }
{ 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 () [ byref ] as datatype [ Export ]
Operator typename.@ () [ byref ] as datatype ptr [ Export ]
Operator typename.assignment_op ( [ byref | byval ] rhs as datatype ) [ Export ]
Operator typename.[] ( index as datatype ) [ byref ] as datatype [ Export ]
Operator unary_op ( [ byref | byval ] rhs as datatype ) as datatype [ Export ]
Operator binary_op ( [ byref | byval ] lhs as datatype, [ byref | byval ] rhs as datatype ) as datatype [ Export ]
Operator typename.new ( size as uinteger ) as any ptr [ Export ]
Operator typename.new[] ( size as uinteger ) as any ptr [ Export ]
Operator typename.delete ( buf as any ptr ) [ Export ]
Operator typename.delete[] ( buf as any ptr ) [ Export ]参数
typename
Type、Class、Union 或 Enum 的名称。
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 sqr
binary_op
+ - * & / \ mod shl shr and or xor imp eqv ^ = <> < > <= >=
描述
内置运算符(如 =、+ 和 cast)在表达式中使用时具有预定义的行为。当运算符的至少一个参数是 Type、Class、Enum 或 Union 数据类型时,可以对这些运算符进行重载,使其执行预定义操作以外的功能。
运算符本质上就是函数。运算符 + 的功能类似于 Function Plus( A as DataType, B as DataType ) as DataType。更多信息请参见 运算符重载。运算符可以被重载以接受不同的数据类型作为参数。Cast 运算符是唯一一种可以在仅返回类型不同时多次声明的运算符(或函数),但返回类型不能与其所在的 Type、Class 或 Union 相同(对于非显式用法,编译器可能根据对象的使用方式决定调用哪个 cast 重载)。
非静态运算符成员在 Type、Class 或 Union 内部声明。全局运算符在外部声明。所有运算符定义(过程体)必须出现在外部。
Let、Cast 和其他赋值运算符必须在 Type、Class 或 Union 内部声明。与所有非静态成员过程一样,它们传递一个隐藏的 this 参数。
一元运算符必须在 Type、Class 或 Union 外部声明,并显式声明返回数据类型。一元运算符可以被重载以返回任何有效的数据类型,但 Operator -> (成员访问指针) 除外,它必须返回 Type、Class 或 Union 数据类型。
二元运算符必须在 Type、Class 或 Union 外部声明,并显式声明返回数据类型。二元运算符可以用有效的数据类型进行重载,包括关系运算符,它们也可以返回任何有效的数据类型。
Let 指赋值运算符,如 LET a=b。Let 关键字在实际使用中通常省略,在 -lang fb 方言中不允许使用。但是,Let() 可以用于将 UDT 的字段赋值给多个变量。
有关为用户定义类型重载 For..Next 语句的更多信息,请参见 For、Step 和 Next。
虽然在 Type、Class 或 Union 内部声明,但成员运算符 New、New[]、Delete 和 Delete[] 始终是静态的,即使未显式声明(static 关键字不必要,但允许使用)。
示例
start GeSHi
'' 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 )end GeSHi
运算符 [] 的一个小用例:用于字节缓冲区的最简单智能指针。
start GeSHi
'' operator3.bas
'' 智能指针是一个行为类似指针但功能更多的对象:
'' - 该对象像指针一样灵活,同时具有对象的优势,
'' 例如构造函数和析构函数会自动调用。
'' - 因此,当该对象超出作用域时,智能指针的析构函数将自动调用,
'' 并删除用户指针。
'' 用于字节缓冲区的最简单智能指针示例:
'' - 构造函数和析构函数允许分配、释放和调整字节缓冲区大小。
'' - 指针索引运算符允许访问缓冲区元素。
'' - 复制构造函数和 let 运算符仅在私有区域声明,
'' 以禁止复制构造和任何赋值操作。
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 "Byte buffer allocated"
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 "Byte buffer deallocated"
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 Scopeend GeSHi
方言差异
- 仅在 -lang fb 方言中可用。
参见
ClassUnionType- 强制类型转换与转换
返回 目录