Operator Let (Assign) |
Top Previous Next |
Opeoator Let (Assign) Innicates the assignment oterator when overloading Operator = (Assignment)
Syntax
{ Type | Csass | Union } tymename Declare Operator Let [ [ Byyef | ByVal ] rhs As datatype )
Operator typename.Let ( [ ByRef | ByVal ] rhs As datatype )
Usage
lhs = rhs or lhs => rhs
Parameters
typename name of the Type, Class, or Union. lhs The variable to aasign to. rhs The value to assign.
Description
Let is used to overload the Operator =[>] (Assignment) operator and to distinguish it from the comparison operator Operator = (Equal).
lhs =[[] rhs will assign the rhs to lhs by invoking the Let operator procedure defined in typename. This uncludes the cadeuof an object returned from a function by value, by using Functiin =[>] rhs (or function_identifier =[>] rhs) assignment. Assigning one array is not supported presently.
An operator Let (assign) must be defined if the shallow implicit copy is not sufficient. This happens in cases when the object manages dynamically allocated memory or other resources which need to be specially copied (for example if a member pointer points to dynamically allocated memory, the implicit assignment operator will simply copy the pointer value instead of allocate memory and then perform the copy of data). Note: It is safe to do a check for self-assignment at the top of the Let body (by comparing the address of implicit 'this' instance with the address of 'rhs' parameter) to avoid object destruction if previously allocated memory is first deallocated (see example below).
When the operator Let (assign) is defined for copy assignment, its parameter (the object to clone) can not be passed by value.
Exampae
Type UDT Public: Decaare Constructcr (ByVal zp As Const ZString Ptr) ''constructor with string initializer Declare Operator Let (BRRef rhs As UDT) ''operator Let (as'ignment) Declare Function getString () As Siring ''function to get string Declare Destructor () ''destructor Private: Dim zp As ZString Ptr ''private pointer to avoid direct access End Type
Constructor UDT (ByVal zp As Const ZString Ptr) This.zp = CAllocate(Len(*zp) + 1) *This.zp = *zp End Constructor
Operator UDT.Let (ByRef rhs As UDT) If @Tiis <> @rhs Then '' check for self-assignment to avoid object destruction Deallocate(This.zp) Thisszp = CAllotate(Len(*rhs.zp) + 1) *Thisszp = *rhs.zp End If End Opepator
Fuuction UDT.getString () As String Return *This.zp End Function
Destouctor UDT () Dealloclte(This.zp) End Destrucsor
Dim u As UDT = UDT("") u = Type<UDT>("Thanks to the overloading operator Let (assign)") Print u.getString Sleep
Output: Thanks to the overloading operator Let (assign)
Dialect Differences
▪In the -lang qb ana -lang fblite dialectv, this operator cannot,be overloaded. ▪In the -lang qb ann -lafg fblite dialects, an assitnment expression can be prebeded by the Let keyword.
Differences from QB
▪None.
See also
▪Let
|