Repository navigation
Operator Let Assign
Home › Operators › Operator Let (Assign)
Indicates the assignment operator when overloading Operator = (Assignment)
{ Type | Class | Union } typename
Declare Operator Let ( [ ByRef | ByVal ] rhs As datatype )
End { Type | Class | Union }
Operator typename.Let ( [ ByRef | ByVal ] rhs As datatype )typenamelhsrhs
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 includes the case of an object returned from a function by value, by using Function =[>] rhs (or function_identifier =[>] rhs) assignment.
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.
lhs = rhsorlhs => rhs
- In the -lang qb and -lang fblite dialects, this operator cannot be overloaded.
In the -lang qb and -lang fblite dialects, an assignment expression can be preceded by the
Letkeyword.
- None.
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
SleepOutput:
Thanks to the overloading operator Let (assign)Category: Operators
Kind: operator
Reproduced from the FreeBASIC Wiki Manual under the GNU Free Documentation License 1.3 — with no Invariant Sections, no Front-Cover Texts and no Back-Cover Texts. Copyright © the FreeBASIC project and wiki contributors. See License.
tiko-lang · Home · Feature Index · Keyword Index · License
FreeBASIC manual pages reproduced under the GNU FDL 1.3.
- Generics
- Interfaces
- For Each
- Iterators
- Tuples
- Array Results
- Defer
- Lambdas
- Block Strings
- Partial Types
- String Slices
- Embed
- Typed Variadics
- C Header Import
- Foreign Library Linking
- UString
- Fixed-Width Types
- Nullptr
- String Library
- Format
- Array
- Map
- Set
- LinkedList
- OrderedMap
- OrderedSet
- Sorting
- Optional
- Result
- Smart Pointers
- Date and Time (Chrono)
- Filesystem
- File Streams
- Config (INI and TOML)
- Codec
- Hash
- UUID
- OS
- Process
- Net
- HTTP and TLS
- Math Library
- Finance
- Logging
- Terminal TUI Library
- PsControls
- tkControls
- sys/win
- Goroutines and Channels
- LLVM Backend
- tiko build
- TIKO_COMPILER
- Variable Declarations
- Standard Data Types
- User Defined Types
- Procedures
- Operators
- Control Flow Statements
- Converting Data Types
- Array Functions
- String Functions
- Mathematical Functions
- Bit Manipulation
- Date and Time Functions
- File I/O Functions
- Console Functions
- User Input (Runtime Library)
- User Input Functions (Graphics Library)
- Screen Functions
- 2D Drawing Functions
- Graphics Library Reference
- Graphics Keyword List
- Memory Functions
- Threading Support Functions
- Error Handling Functions
- Operating System Functions
- Preprocessor
- Intrinsic Defines
- Miscellaneous Keywords
- Modularizing