Revision [16524]

This is an old revision of KeyPgOpLet made by FxMwikki on 2013-01-03 15:00:50.

 

Operator Let (Assign)


Indicates the assignment operator when overloading KeyPgOpAssignment Operator = (Assignment)

Syntax:
Usage:
lhs = rhs

Parameters:
typename
lhs
The variable to assign to.
rhs
The value to assign.

Description:
Let is used to overload the KeyPgOpAssignment Operator = (Assignment) operator and to distinguish it from the comparison operator KeyPgOpEqual Operator = (Equal).
lhs = rhs will assign the rhs to lhs by invoking the Let operator procedure defined int typename.
An operator Let (assign) must be defined if the shallow default 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 default 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).

Examples:
Type UDT
  Public:
    Declare Constructor (ByVal zp As Const ZString Ptr)  ''constructor with string initializer
    Declare Operator Let (ByRef rhs As UDT)              ''operator Let (assignment)
    Declare Function getString () As String              ''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 @This <> @rhs Then  '' check for self-assignment to avoid object destruction
    Deallocate(This.zp)
    This.zp = CAllocate(Len(*rhs.zp) + 1)
    *This.zp = *rhs.zp
  End If
End Operator

Function UDT.getString () As String
  Return *This.zp
End Function

Destructor UDT ()
  Deallocate(This.zp)
End Destructor


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:
Differences from QB:
See also:
Back to Assignment Operators
Back to Operators
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki



sf.net phatcode