Repository navigation
Cast
Home › Converting Data Types › Cast
Converts a value to any named type, such as Cast(Byte, flag).
Cast( datatype, expression )datatypeexpression
Converts expression into a different datatype. Useful to be used in macros when datatype is unknown and also when converting to Type Alias.
This is a general form of conversion operators such as CInt or CDbl.
Cast is more versatile because it can be used on built-in types that have a built-in Cast Operator, but don't have a built-in keyword for it: e.g. Cast( Byte, boolean ).
It is also suitable for use in cases where the type of a variable is not fixed in the code - for example, it might be Defined earlier, or may be the Type Of a different variable or expression.
Note: If you want to use an operator specifically for converting to different types of Pointers, consider using CPtr instead.
Upcasting: In an inheritance structure, the upcasting is converting a derived type reference or pointer into a base type.
In this special use, Cast applied on a derived-type instance (expression) can be used to return a base-type (datatype) reference.
By using a member Operator Cast, Cast can be overloaded for a user defined type expression.
- Not available in the -lang qb dialect unless referenced with the alias
__Cast.
- New to FreeBASIC
'' will print -128 because the integer literal will be converted to a signed Byte
'' (this Casting operation is equivalent to using CByte)
Print Cast( Byte, &h0080 )
'' will print 3 because the floating-point value will be converted to an Integer
'' (this Casting operator is equivalent to using CInt)
Print Cast( Integer, 3.1 )
Sleep'' macro sizeofDerefPtr(): returns the size of the dereferenced pointer
#define sizeofDerefPtr(ptrToDeref) SizeOf(*Cast(TypeOf(ptrToDeref), 0))
'' macro typeofDerefPtr(): returns the type of the dereferenced pointer
#define typeofDerefPtr(ptrToDeref) TypeOf(*Cast(TypeOf(ptrToDeref), 0))
' Allocate dynamically memory for a Double by New
Dim As Double Ptr pd
pd = New typeofDerefPtr(pd)
*pd = 3.14159
Print *pd
' Allocate dynamically memory for a Zstring*10 by Callocate
Dim As ZString Ptr pz
pz = CAllocate(10, sizeofDerefPtr(pz))
*pz = "FreeBASIC"
Print *pz
Sleep
Delete pd
Deallocate(pz)Category: Converting Data Types
Kind: keyword
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.
- Home
- What Is tiko-lang
- Quick Start
- Installing on macOS
- Debugging
- Feature Index
- Compatibility
- FAQ
- AI Cheatsheet
- 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)
- Time Zones
- 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