Revision history for KeyPgByrefVariables


Revision [21695]

Last edited on 2016-11-12 01:19:11 by fxm [Formatting]
Additions:
For example, a base type reference referring to a derived type object allows to activate polymorphism when a virtual method is called on it, similarly to a base type pointer referring to a derived type object. One can do the same operations through such a reference as one can do with a dereferenced pointer of same type (but for both not the same operations as using directly the derived type instance).
Deletions:
For example, a base type reference referring to a derived type object allows to activate polymorphism when a virtual method is called on it, similarly to a base type pointer referring to a derived type object. One can do the same operations through such a reference as one can do with a dereferenced pointer of same type (but for both not the same operations as using directly the derived type instance).


Revision [21693]

Edited on 2016-11-11 08:03:45 by fxm [Rewording]
Additions:
- Otherwise one cannot do all same operations than with the original variable:
For example, a base type reference referring to a derived type object allows to activate polymorphism when a virtual method is called on it, similarly to a base type pointer referring to a derived type object. One can do the same operations through such a reference as one can do with a dereferenced pointer of same type (but for both not the same operations as using directly the derived type instance).
Deletions:
- Otherwise one cannot do all same operations than with the original variable. For example, a base type reference referring to a derived type object allows to activate polymorphism when a virtual method is called on it, similarly to a base type pointer referring to a derived type object. One can do the same operations through such a reference as one can do with a dereferenced pointer of same type (but for both not the same operations as using directly the derived type instance).


Revision [21692]

Edited on 2016-11-11 07:39:32 by fxm [Rewording]
Additions:
- Otherwise one cannot do all same operations than with the original variable. For example, a base type reference referring to a derived type object allows to activate polymorphism when a virtual method is called on it, similarly to a base type pointer referring to a derived type object. One can do the same operations through such a reference as one can do with a dereferenced pointer of same type (but for both not the same operations as using directly the derived type instance).
Deletions:
- Otherwise for example, a base type reference referring to a derived type object allows to activate polymorphism when a virtual method is called on it, similarly to a base type pointer referring to a derived type object. One can do the same operations through such a reference as one can do with a dereferenced pointer of same type (but for both not the same operations than with a derived type instance).


Revision [21689]

Edited on 2016-11-09 06:33:02 by fxm [Rewording]
Additions:
- When the two types are identical (or using the third syntax with ##**Var**##), a reference can be considered as an alias of the variable. One can do the same operations through such a reference as one can do with the original variable.
Deletions:
- When the two types are identical (or using the third syntax with ##**Var**##), a reference can be considered as a perfect alias of the variable. One can do the same operations through such a reference as one can do with the original variable.


Revision [21688]

Edited on 2016-11-09 05:34:27 by fxm [Rewording]
Additions:
[[DataType|DataType]] must be the same type as that of the variable, or a compatible type (for example one from the types of its Bases in case of inheritance):
- Otherwise for example, a base type reference referring to a derived type object allows to activate polymorphism when a virtual method is called on it, similarly to a base type pointer referring to a derived type object. One can do the same operations through such a reference as one can do with a dereferenced pointer of same type (but for both not the same operations than with a derived type instance).
Deletions:
[[DataType|DataType]] must be the same type as that of the variable, or one of the types of its Bases in case of inheritance:
- Otherwise, a base type reference referring to a derived type object allows to activate polymorphism when a virtual method is called on it, similarly to a base type pointer referring to a derived type object. One can do the same operations through such a reference as one can do with a dereferenced pointer of same type (but for both not the same operations than with a derived type instance).


Revision [21629]

Edited on 2016-10-30 17:32:39 by fxm [Formatting]
Additions:


Revision [21318]

Edited on 2016-04-05 02:29:31 by fxm [Wording]
Additions:
[[DataType|DataType]] must be the same type as that of the variable, or one of the types of its Bases in case of inheritance:
- Otherwise, a base type reference referring to a derived type object allows to activate polymorphism when a virtual method is called on it, similarly to a base type pointer referring to a derived type object. One can do the same operations through such a reference as one can do with a dereferenced pointer of same type (but for both not the same operations than with a derived type instance).
Deletions:
[[DataType|DataType]] must be the same type as that of the variable or one of the types of its Bases (if inheritance):
- Otherwise, a base type reference referring to a derived type object allows to activate polymorphism when a virtual method is called on it, similarly to a base type pointer referring to a derived type object. One can do the same operations through such a reference as one can do with a dereferenced pointer of same type (for both not the same operations than with a derived type instance).


Revision [21301]

Edited on 2016-04-02 09:48:19 by fxm [Correcting duplicated file name for examples 3 and 4]
Additions:
{{fbdoc item="filename" value="examples/manual/variable/byref4.bas"}}%%(freebasic)


Revision [21300]

Edited on 2016-04-02 02:47:24 by fxm [Wording]
Additions:
- Otherwise, a base type reference referring to a derived type object allows to activate polymorphism when a virtual method is called on it, similarly to a base type pointer referring to a derived type object. One can do the same operations through such a reference as one can do with a dereferenced pointer of same type (for both not the same operations than with a derived type instance).
Deletions:
- Otherwise, a base type reference referring to a derived type object allows to activate polymorphism when a virtual method is called on it, similarly to a base type pointer referring to a derived type object. One can do the same operations through such a reference as one can do with a dereferenced pointer of same type (not the same operations than with a derived type instance).


Revision [21299]

Edited on 2016-04-02 02:38:36 by fxm [Wording]
Additions:
- Otherwise, a base type reference referring to a derived type object allows to activate polymorphism when a virtual method is called on it, similarly to a base type pointer referring to a derived type object. One can do the same operations through such a reference as one can do with a dereferenced pointer of same type (not the same operations than with a derived type instance).
Deletions:
- Otherwise, a base type reference referring to a derived type object allows to activate polymorphism when a virtual method is called on it, similarly to a base type pointer referring to a derived type object. One can do the same operations through such a reference as one can do with a dereferenced pointer of same type (not the same operations than with the original variable).


Revision [21281]

Edited on 2016-04-01 07:33:53 by fxm [Created new page "BYREF (variables)" for dim'ing references]
Additions:
{{fbdoc item="title" value="BYREF (variables)"}}----
Declares a reference
{{fbdoc item="syntax"}}
##([[KeyPgDim|Dim]] | [[KeyPgStatic|Static]]) [[[KeyPgShared|Shared]]] **Byref** //name1// As [[DataType|DataType]] = //variable1// [, **Byref** //name2// As [[DataType|DataType]] = //variable2//, ...]##
//or//
##([[KeyPgDim|Dim]] | [[KeyPgStatic|Static]]) [[[KeyPgShared|Shared]]] **Byref** As [[DataType|DataType]] //name1// = //variable1// [, //name2// = //variable2//, ...]##
//or//
##[[KeyPgVar|Var]] [[[KeyPgShared|Shared]]] **Byref** As [[DataType|DataType]] //name1// = //variable1// [, **Byref** //name2// = //variable2//, ...]##
{{fbdoc item="param"}}
##//name//##
reference name
##//variable//##
variable name to refer
Declares a reference (by name) to a variable.
A reference is an entity that is a way to access data located in memory. A reference is not the data itself but only information about its location. A reference can be thought of as a pointer that is implicitly dereferenced. In many cases, it can be used as an alternative to pointer.
A reference must always be initialized with a variable when it is created.
[[DataType|DataType]] must be the same type as that of the variable or one of the types of its Bases (if inheritance):
- When the two types are identical (or using the third syntax with ##**Var**##), a reference can be considered as a perfect alias of the variable. One can do the same operations through such a reference as one can do with the original variable.
- Otherwise, a base type reference referring to a derived type object allows to activate polymorphism when a virtual method is called on it, similarly to a base type pointer referring to a derived type object. One can do the same operations through such a reference as one can do with a dereferenced pointer of same type (not the same operations than with the original variable).
A reference can be reassigned to refer to another variable (of compatible type) by doing:
@//refname// = @//othervariable//
NOTE: The arrays of references and the non-static reference fields for UDT are not supported yet.
{{fbdoc item="filename" value="examples/manual/variable/byref1.bas"}}%%(freebasic)
'' Comparison between:
'' - a copy ('ci') of a variable ('i')
'' - a reference ('ri') to a variable ('i')
Dim As Integer i = 12
Print @i, i
Dim As Integer ci = i '' or Var ci = i
Print @ci, ci
Dim Byref As Integer ri = i '' or Var Byref ri = i
Print @ri, ri
Print
Print i, ci, ri
i = 34
Print i, ci, ri
ci = 56
Print i, ci, ri
ri = 78
Print i, ci, ri
Sleep
{{fbdoc item="filename" value="examples/manual/variable/byref2.bas"}}%%(freebasic)
'' Use reference allows to simplify expressions compared to pointer
'' (avoid to use operator '@' and especially '*')
Dim As Zstring Ptr pz = @"FreeBASIC Zstring Ptr"
Print *pz
*pz &= " 1.3.0"
Print *pz
Print
Dim Byref As Zstring rz = "FreeBASIC Zstring Ref" '' or Var Byref rz = "FreeBASIC Zstring Ref"
Print rz
rz &= " 1.4.0"
Print rz
Sleep
{{fbdoc item="filename" value="examples/manual/variable/byref3.bas"}}%%(freebasic)
'' It is possible to reassign a reference.
'' An example with an UDT to control the successive constructions & destructions of objects handled with one only reference.
Type UDT
Declare Constructor ()
Declare Destructor ()
Dim As Integer I
End Type
Constructor UDT ()
Static As Integer nb
nb += 1
This.I = nb
Print "UDT.Constructor()"
End Constructor
Destructor UDT ()
Print "UDT.Destructor()"
End Destructor
Var Byref ru = *New UDT '' or Dim Byref As UDT ru = *New UDT
Print ru.I
Delete @ru
Print
@ru = New UDT
Print ru.I
Delete @ru
Sleep
{{fbdoc item="filename" value="examples/manual/variable/byref3.bas"}}%%(freebasic)
'' Polymorphism (by using inheritance and virtuality) can be activated through any of the 3 following kinds of entities:
'' - base-type pointers referring to derived-type objects,
'' - dereferenced base-type pointers referring to derived-type objects,
'' - base-type references referring to derived-type objects.
'
'' If in the first line of the below code, FALSE is put instead TRUE, the polymorphism by virtuality is no more activated.
#define virtuality TRUE
Type myBase Extends Object
#if virtuality = TRUE
Declare Virtual Sub hello()
#else
Declare Sub Hello()
#endif
End Type
Sub myBase.hello()
Print "myBase.hello()"
End Sub
Type myDerived Extends myBase
Declare Sub hello()
End Type
Sub myDerived.hello()
Print "myDerived.hello()"
End Sub
Dim As myBase mb
Dim As myBase Ptr pmb = @mb
Dim Byref As myBase rmb = mb '' or Var Byref rmb = mb
pmb->hello() '' pmb is a base-type pointer referring to a base-type object
(*pmb).hello() '' *pmb is a dereferenced base-type pointer referring to a base-type object
rmb.hello() '' rmb is a base-type reference referring to a base-type object
Print
Dim As myDerived md
Dim As myBase Ptr pmd = @md
Dim Byref As myBase rmd = md '' only syntax because the reference data-type must be different from the one of object
pmd->hello() '' pmd is a base-type pointer referring to a derived-type object
(*pmd).hello() '' *pmd is a dereferenced base-type pointer referring to a derived-type object
rmd.hello() '' rmd is a base-type reference referring to a derived-type object
Sleep
{{fbdoc item="lang"}}
- Only supported in -lang fb, -lang fblite and -lang deprecated dialects.
- New to ""FreeBASIC"".
- ##[[KeyPgDim|Dim]]##
- ##[[KeyPgStatic|Static]]##
- ##[[KeyPgVar|Var]]##
- ##[[KeyPgShared|Shared]]##
- ##[[KeyPgByrefFunction|Byref (function results)]]##
{{fbdoc item="back" value="CatPgVariables|Variable Declarations"}}
Deletions:
{{fbdoc item="title" value="BYREF (function results)"}}----
Specifies that a function result is returned by reference
{{fbdoc item="syntax"}}##
[[KeyPgFunction|Function]] //name// ( //parameter-list// ) **Byref** [[KeyPgAs|As]] //[[DataType|datatype]]//
##
Causes the function result to be returned by reference, rather than by value. A function returning ##**Byref**## will return the address of a variable, instead of making a copy like when returning by value. This allows the caller of the function to modify the variable which the function result points to.
If ##**Byref**## is not specified, the default is to return the function result by value.
Functions with ##**Byref**## result should not return local variables from the function, because they will be destroyed upon returning from the function, invalidating any pointer or reference to them. To help with writing safe code, the compiler will show an error message when a local variable is used with ##Function = //x//## (or ##//name// = //x//##) assignments and ##Return //x//## statements.
**Note:** On the left-hand side of an assignment expression using the '##=##' symbol, the result of the function (returned by reference) must be enclosed in parentheses when the function calls one single argument, in order to solve the parsing ambiguity. From fbc version 0.90, '##=>##' can be used for assignments, in place of '##=##', same as for initializers, allowing to avoid parsing ambiguity (without parentheses). As for the arguments list, it should always be surrounded with parentheses even if empty.
Operators (member or global), when used as functions, have also the capability to return results by reference, by using the same syntax.
{{fbdoc item="filename" value="examples/manual/procs/byref-result4.bas"}}%%(freebasic)
Function min( Byref I As Integer , Byref J As Integer ) Byref As Integer
'' The smallest integer will be returned by reference, no copy will be created.
If I < J Then
Return I
Else
Return J
End If
End Function
Dim As Integer A = 13, B = 7
Print A, B
Print min( A , B )
min( A , B ) = 0
Print A, B
{{fbdoc item="filename" value="examples/manual/procs/byref-result.bas"}}%%(freebasic)
function f( ) byref as const zstring
'' This string literal (because statically allocated in memory) will be returned by reference, no copy will be created.
function = "abcd"
end function
print f( )
{{fbdoc item="filename" value="examples/manual/procs/byref-result2.bas"}}%%(freebasic)
Dim Shared As String s
Function f1( ) ByRef As String
'' This variable-length string will be returned by reference, no copy will be created.
Function = s
End Function
Function f2( ByRef _s As String ) ByRef As String
'' This variable-length string will be returned by reference, no copy will be created.
Function = _s
End Function
s = "abcd"
Print s
f1( ) &= "efgh"
Print s
'' At time of writing, the enclosing parentheses are required here.
( f2( s ) ) &= "ijkl"
Print s
{{fbdoc item="filename" value="examples/manual/procs/byref-result3.bas"}}%%(freebasic)
Function power2( Byref _I As Integer ) Byref As Integer
_I *= _I
'' This integer will be returned by reference, no copy will be created.
Function = _I
End function
Dim As Integer I = 2
power2( power2( power2( I ) ) ) '' Function return-byref cascading is equivalent to ((I*I)*(I*I))*((I*I)*(I*I)) = I^8
Print I
- New to ""FreeBASIC""
- [[ProPgReturnValue|Returning values]]
{{fbdoc item="back" value="CatPgProcedures|Procedures"}}


Revision [21280]

The oldest known version of this page was created on 2016-04-01 07:32:04 by fxm [Created new page "BYREF (variables)" for dim'ing references]
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki



sf.net phatcode