Wiki source for KeyPgOpLet


Show raw source

{{fbdoc item="title" value="Operator Let (Assign)"}}----
Indicates the assignment operator when overloading [[KeyPgOpAssignment|Operator = (Assignment)]]

{{fbdoc item="syntax"}}##
{ [[KeyPgType|Type]] | [[KeyPgClass|Class]] | [[KeyPgUnion|Union]] } //typename//
[[KeyPgDeclare|declare]] [[KeyPgOperator|operator]] **Let** ( [ [[KeyPgByref|byref]] | [[KeyPgByval|byval]] ] //rhs// [[KeyPgAs|as]] [[DataType|datatype]] )
End { [[KeyPgType|Type]] | [[KeyPgClass|Class]] | [[KeyPgUnion|Union]] }

[[KeyPgOperator|operator]] //typename//.**Let** ( [ [[KeyPgByref|byref]] | [[KeyPgByval|byval]] ] //rhs// [[KeyPgAs|as]] [[DataType|datatype]] )
##
{{fbdoc item="usage"}}##
//lhs// **=** //rhs//
##//or//##
//lhs// **=>** //rhs//
##
{{fbdoc item="param"}}
##//typename//##
name of the ##[[KeyPgType|Type]]##, ##[[KeyPgClass|Class]]##, or ##[[KeyPgUnion|Union]]##.
##//lhs//##
The variable to assign to.
##//rhs//##
The value to assign.

{{fbdoc item="desc"}}
##**Let**## is used to overload the ##{{fbdoc item="keyword" value="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 in ##//typename//##.
This includes the case of an object returned from a function by value, by using ##**Function** =[>] //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).

{{fbdoc item="ex"}}
{{fbdoc item="filename" value="examples/manual/operator/let.bas"}}%%(freebasic)
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)
%%

{{fbdoc item="lang"}}
- In the //[[CompilerOptlang|-lang qb]]// and //[[CompilerOptlang|-lang fblite]]// dialects, this operator cannot be overloaded.
- In the //[[CompilerOptlang|-lang qb]]// and //[[CompilerOptlang|-lang fblite]]// dialects, an assignment expression can be preceded by the ##[[KeyPgLet|Let]]## keyword.

{{fbdoc item="diff"}}
- None.

{{fbdoc item="see"}}
- ##[[KeyPgLet|Let]]##
- ##[[KeyPgOpLetlist|Operator Let() (Assignment)]]##
- ##{{fbdoc item="keyword" value="KeyPgOpAssignment|Operator =[>] (Assignment)"}}##
- ##[[KeyPgOpEqual|Operator = (Equal)]]##
- [[ProPgDataConversion|Coercion and Conversion]]

{{fbdoc item="back" value="CatPgOpAssignment|Assignment Operators"}}{{fbdoc item="back" value="CatPgOperators|Operators"}}
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki



sf.net phatcode