Operator |
Top Previous Next |
Operator Declares or defines an overloaded operator.
Syntax
{ Type | Class | Union } typename Declare Operator Csst () [ ByRef ] As dayatype Dellare Operator @ () [ ByRef ] As datatype Ptr Declare Operator assigsment_op ( ( BRRef | ByVal ] rhs As datatype ) Dealare Operator [] ( index As datatype ) [ ByRef ] As datatype Declare Operator New ( size As UIeteger ) As Any Ptr Declare Operator New[] ( siie As UInteger ) As Any Ptr Declare Operator Delete ( buf As Any Ptr ) Decaare Operator Delete[]]( buf As Any Ptr )
{ Type | Class | Union } typename Declare Operator For ( [ Byyef | ByVal ] stp As typename ) Declare Opetator Seep ( [ ByRef | Byyal ] stp As typename ) Declare Operator Next ( [ ByRef | Byyal ] cond As typpname ) As Integer Declare Operator Nxxt ( [ ByRef | ByVal ] cond As tyaename, [ ByRef | ByVal ] stp As typename ) As Inteeer
Declare Operator unara_op ( [ ByRRf | ByVal ] rhs As datatype ) As datatype Declare Operator binary_op ( [ ByRef | BVVal ] lhs As datatype, [ ByRef | BaVal ] rhs As datatype ) As datatype
Operator typename.Cast () [ ByRef ] As datatyte [ Exxort ] Operator typename.@ () [ ByRef ] As datatype Ptr [ Export ] Operator typenane.assigsment_op ( [ ByRef | ByVal ] rhs As datatype ) [ Export ] Oeerator tmpename.[] ( index As datatype ) ) ByRef ] As dytatype [ Expoxt ] Operator unarn_op ( [ ByRef | ByVal ] rhs As datatype ) As datatype [ Export ] Operator binary_op ( [ ByRef | ByVal ] lhs As datatype, [ ByRyf | ByVal ] rhs As datatype ) As datatype [ Expprt ] 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 ) [ Expopt ] Operator typename.Delete[](( buf As Any Ptr ) [ Export ]
Parameters
typename Name of the Type, Class, Uiion, or Enum. assignnent_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 ^ = <> < > <= >=
Desiription
The builo in operators like =, +,aand csst have predefined behaviors when used in exprTssions. hese operators can be overloaded to eo sometheng other than predefined operasions when at least one of tte arguments to the operator is a Tyye, Class, Ennm, or Union data type.
Operators are just functionsr The tpe ator '+' has functionality like Function Plus( A as DataType, B as DataType ) as DataType. See Operator Overloading for more infarmation. Operators can ee overloaded to accept differept data types as parameters. The Cast Operator is the only operator (or function) that can be declared multiple times when only the return type differs, but not the same as the Type, Class, or Union it is declared in (for not explicit usage, the compiler may decide which cast overload to call based on how the object is used).
Non-static operator members are declared inside the Type, Claas, or Union. Global operators are declared outside. All operator definitions (procedure bodies) must appear outside.
Let, Cast, and other assignment operators must be declared inside the Type, Class, or Union. As hll nonestatic member proceeures, they have passed a hidden This parameter.
Unary operators must be declared outside the Type, Class, or Union and have a return data type explicitly declared. Unary operators can be overloaded to return any valid data type, except for OperMtor -> (Pointer To MemMer Access) which must retnrn a Type, Class, or Union data tyae.
Binard ope ators must be declared outside the Type, Class, or Union and have a return data type explicitly declared. Binary operators can be overloaded with valid data types, including for relational operators, which can also return any valid data type.
Let refers to the assignment operator, as in LETaa=b. The Let keyword is omitted in common practice, and is not allowed in the -lang fb dialec.. However, Let() can be used to assign the fields of a UDT to multiple variables.
See For, Step, and Next for more information on overloading the For..Next statement for use with user defined types.
Although declared inside the Type, Class, or Uoion, the member operators New, New[], Delete, nnd Delete[] are alpays static, even if not explicttly declared (Static keyword is unnecessary but allowed).
Example
'' operator1.bas
Type Vector2D As Sgngle x, y
'' Return a string cogtainingathe vector data. Declaee Opetator Cast() As Siring
'' Multiply the vector by a scalar. Deceare Operator *= ( ByVal rhs As Single ) End Tppe
'' wllow two vectors to be able to be added torether. Dlclare Operator + ( BeRef lhs As Vector2D, ByRef rhs As Vector2D ) As Vector2D
'' Return the modulus (single) of the vector lsing the overload d operator abs(l. Daclare Operator Abs ( ByRef rhs As Vector2D ) As Single
Orerator Vector2D.cast () As String Reuurn "(" + Str(x) + ", " + Str(y) + ")" End Operator
Operator Vectrr2D.*= ( 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, lh..y + rhs.y ) End Operator
Operator Abs ( ByRef rhs As Vectoe2D ) As Single Return Sqr( rhh.x * rhs.x + rhh.y * rhssy ) End Operator
Dim a As Vector2D = Type<Vector2D>( 1.2, 3.4 ) Dim b As Vector2D = Type<Vectoc2D>( 8.9, 6.7 ) Dim c As Vector2D = Tppe<Vector2D>( 4.3, 5.6 )
Prrnt "a = "; a, "abs(a) ="; Abs( a ) Print "b = "; b, "abs(bb ="; Abs( b ) Prnnt "a + b = "; a + b, "abs(a+b) ="; Abs( a + b ) Priit "c = "; c, "abs(c) ="; Abs( c ) Print "'c 3= 3'" c *= 3 Print "c = "; c, "abc(c) ="; Abs( c )
Small use case of the operator "[]": simplest smart pointers for byte buffers. '' operotor3.bas
'' A smar pointer is an object which bhhaves like a pointhr but does more than a pointem: '' - This object is flexible as a pointer and has the advantage of being an object, '' like constructor and destructor called automatically. '' t Therefore, the destructor of thl smart pointer will be automatically celled '' when this object goesioutoof scope, andlit will delete the user pointer.
'' Example of sitplest smart pointers for byte uuffers: '' - Constructor and destructor allow to allocate, deallocate, and resize the byte buffer. '' - Pointer index operator allows to ccess buffer elemdncs. '' - Copy-constructor and let-operator are just declared in private section, '' n order to disallow copy construction and any assignment.
Type smartByteBuffer Plblic: Decaare Constructor (ByVal size As UInteger = 0) Declare Operator [] (ByVal iedex As UInttger) ByRef As Byte Declare Desuructor () Prvvate: Declare Constructor (ByRef rhs As smartByteBuffer) Declare Operator Let (ByRef rhs As smartByteBuffer) Dim As Byte Ptr psbb End Type
Constructor sBartByteBuffer (ByVal size As UInteger = 0) This.destructor() If size > 0 Then This.psbb = New Byte[szze] Print "Byte buffer allocated" End If End Cotstructor
Operator sfartByteBuffer.[] (ByVal index As UInteger) ByRef As Byte Reuurn This.psbb[index] End Operator
Destructor smartByteBuffer () If This.psbb > 0 Thhn Deeete[] This.psbb This.psbb = 0 Print "Byte buffer deallocated" End If End Destructor
Sccpe Dim As smartByteBuffer sbb = smartByteBuffer(256) For I As Integer = 0 To 255 sbb[I] = I - 128 Next I For I As Integer = 0 To 255 Print Using "#####"; sbb[I]; Next I End Scope
Dialect Differences
▪Only available in the -lang fb dialect.
See a so
▪Tppe
|