Revision history for KeyPgConstructor


Revision [21744]

Last edited on 2017-01-29 00:52:16 by fxm [Wording]
Additions:
Chaining together constructors of a same type is allowed by using the keyword ##**Constructor**(//parameters//)## at the top of constructor. It prevents the compiler from emitting field initialization code (instead, it relies on the chained constructor to initialize everything).
Deletions:
The keyword ##**Constructor**(//parameters//)## can be used at the top of a constructor, allowing to chain together constructors of same type. It prevents the compiler from emitting field initialization code (instead, it relies on the chained constructor to initialize everything).


Revision [21631]

Edited on 2016-10-30 17:45:11 by fxm [Formatting]
Additions:


Revision [21369]

Edited on 2016-04-15 08:14:45 by fxm [Added link to 'Coercion and Conversion']
Additions:
- [[ProPgDataConversion|Coercion and Conversion]]


Revision [20858]

Edited on 2016-03-12 16:00:54 by fxm [Formatting]
Additions:









Revision [20103]

Edited on 2016-02-10 15:56:01 by DkLwikki [Update link format]
Additions:
[[KeyPgType|Type]] //typename//
[[KeyPgDeclare|declare]] **Constructor** ( )
[[KeyPgDeclare|declare]] **Constructor** ( [ [[KeyPgByref|byref]] | [[KeyPgByval|byval]] ] //parameter// [[KeyPgAs|as]] [[DataType|datatype]] [ = //default// ] [, ... ] )
**Constructor** //typename// ( [ //parameters// ] ) [ [[KeyPgExport|Export]] ]
name of the ##[[KeyPgType|Type]]## or ##[[KeyPgClass|Class]]##
##**Constructor**## methods are called when a user defined ##[[KeyPgType|Type]]## or ##[[KeyPgClass|Class]]## variable is created.
##//typename//## is the name of the type for which the ##**Constructor**## method is declared and defined. Name resolution for ##//typename//## follows the same rules as procedures when used in a ##[[KeyPgNamespace|Namespace]]##.
A constructor method is passed a hidden ##[[KeyPgThis|this]]## parameter having the same type as ##//typename//##. ##[[KeyPgThis|this]]## is optionally used to access the fields of the ##[[KeyPgType|Type]]## or ##[[KeyPgClass|Class]]## which is to be initialized in the ##**Constructor**## method.
Constructors are called when declaring global or local static instances of ##//typename//## and when allocating ##//typename//## dynamically using the ##[[KeyPgOpNew|New]]## operator. See examples below for different constructor syntaxes.
A copy ##**Constructor**## is a special constructor that initializes a new object from an existing object. There are three general cases where the copy ##**Constructor**## is called: when instantiating one object and initializing it with another object (in one instruction), when passing an object by value, when an object is returned from a function by value (by using ##[[KeyPgReturn|Return]] //x//## statement).
Note: When an object is returned from a function by value, but by using ##**Function** = //x//## (or ##//function_identifier// = //x//##) assignment, the ##**Constructor**## is called once at first, and then the ##[[KeyPgOpLet|Let (Assign)]]## operator at each assignment.
##**Constructor**## can be also called directly from the ##//typename//## instance like the other member methods (##[[KeyPgSub|Sub]]##) and with the same syntax, i.e. using a member access operator, e.g. ##//obj//.**Constructor**(//parameters//)##. In particular, doing ##//this//.**Constructor**(//parameters//)## is not treated as chaining constructor, and it is allowed anywhere (not only at the top of constructors). In general it's not safe to manually call the constructor on an object, because no ##[[KeyPgDestructor|destructor]]## is called, and the old object state - if any - is overwritten without any of its old members being destroyed, which could cause memory/resource leaks.
- Object-related features are supported only in the //[[CompilerOptlang|-lang fb]]// option
- ##[[KeyPgClass|Class]]##
- ##[[KeyPgModuleConstructor|Constructor (Module)]]##
- ##[[KeyPgOpNew|New]]##
- ##[[KeyPgDestructor|Destructor]]##
- ##[[KeyPgType|Type]]##
Deletions:
[[KeyPgType Type]] //typename//
[[KeyPgDeclare declare]] **Constructor** ( )
[[KeyPgDeclare declare]] **Constructor** ( [ [[KeyPgByref byref]] | [[KeyPgByval byval]] ] //parameter// [[KeyPgAs as]] [[DataType datatype]] [ = //default// ] [, ... ] )
**Constructor** //typename// ( [ //parameters// ] ) [ [[KeyPgExport Export]] ]
name of the ##[[KeyPgType Type]]## or ##[[KeyPgClass Class]]##
##**Constructor**## methods are called when a user defined ##[[KeyPgType Type]]## or ##[[KeyPgClass Class]]## variable is created.
##//typename//## is the name of the type for which the ##**Constructor**## method is declared and defined. Name resolution for ##//typename//## follows the same rules as procedures when used in a ##[[KeyPgNamespace Namespace]]##.
A constructor method is passed a hidden ##[[KeyPgThis this]]## parameter having the same type as ##//typename//##. ##[[KeyPgThis this]]## is optionally used to access the fields of the ##[[KeyPgType Type]]## or ##[[KeyPgClass Class]]## which is to be initialized in the ##**Constructor**## method.
Constructors are called when declaring global or local static instances of ##//typename//## and when allocating ##//typename//## dynamically using the ##[[KeyPgOpNew New]]## operator. See examples below for different constructor syntaxes.
A copy ##**Constructor**## is a special constructor that initializes a new object from an existing object. There are three general cases where the copy ##**Constructor**## is called: when instantiating one object and initializing it with another object (in one instruction), when passing an object by value, when an object is returned from a function by value (by using ##[[KeyPgReturn Return]] //x//## statement).
Note: When an object is returned from a function by value, but by using ##**Function** = //x//## (or ##//function_identifier// = //x//##) assignment, the ##**Constructor**## is called once at first, and then the ##[[KeyPgOpLet Let (Assign)]]## operator at each assignment.
##**Constructor**## can be also called directly from the ##//typename//## instance like the other member methods (##[[KeyPgSub Sub]]##) and with the same syntax, i.e. using a member access operator, e.g. ##//obj//.**Constructor**(//parameters//)##. In particular, doing ##//this//.**Constructor**(//parameters//)## is not treated as chaining constructor, and it is allowed anywhere (not only at the top of constructors). In general it's not safe to manually call the constructor on an object, because no ##[[KeyPgDestructor destructor]]## is called, and the old object state - if any - is overwritten without any of its old members being destroyed, which could cause memory/resource leaks.
- Object-related features are supported only in the //[[CompilerOptlang -lang fb]]// option
- ##[[KeyPgClass Class]]##
- ##[[KeyPgModuleConstructor Constructor (Module)]]##
- ##[[KeyPgOpNew New]]##
- ##[[KeyPgDestructor Destructor]]##
- ##[[KeyPgType Type]]##


Revision [17844]

Edited on 2015-11-02 08:31:07 by FxMwikki [Added optional specifier 'Export' in keyword syntax]
Additions:
**Constructor** //typename// ( [ //parameters// ] ) [ [[KeyPgExport Export]] ]
Deletions:
**Constructor** //typename// ( [ //parameters// ] )


Revision [17745]

Edited on 2015-08-13 14:35:25 by FxMwikki [Corrected typo]
Additions:
The keyword ##**Constructor**(//parameters//)## can be used at the top of a constructor, allowing to chain together constructors of same type. It prevents the compiler from emitting field initialization code (instead, it relies on the chained constructor to initialize everything).
Deletions:
The keyword ##**Constructor**(//parameters//)## can be used at the top of a constructor, allowing to chain together constructors of same type. It prevents the compiler from emitting field initalization code (instead, it relies on the chained constructor to initialize everything).


Revision [17008]

Edited on 2014-03-08 16:18:55 by FxMwikki [Formatting]

No Differences

Revision [17007]

Edited on 2014-03-08 14:43:47 by FxMwikki [Constructor() can be used at the top of a constructor to chain together constructors of same type]
Additions:
The keyword ##**Constructor**(//parameters//)## can be used at the top of a constructor, allowing to chain together constructors of same type. It prevents the compiler from emitting field initalization code (instead, it relies on the chained constructor to initialize everything).
##**Constructor**## can be also called directly from the ##//typename//## instance like the other member methods (##[[KeyPgSub Sub]]##) and with the same syntax, i.e. using a member access operator, e.g. ##//obj//.**Constructor**(//parameters//)##. In particular, doing ##//this//.**Constructor**(//parameters//)## is not treated as chaining constructor, and it is allowed anywhere (not only at the top of constructors). In general it's not safe to manually call the constructor on an object, because no ##[[KeyPgDestructor destructor]]## is called, and the old object state - if any - is overwritten without any of its old members being destroyed, which could cause memory/resource leaks.
Deletions:
##**Constructor**## can be also called directly from the ##//typename//## instance like the other member methods (##[[KeyPgSub Sub]]##) and with the same syntax, i.e. using a member access operator, e.g. ##//obj//.**Constructor**(//parameters//)##. No ##[[KeyPgDestructor destructor]]## is called, and the old object state - if any - is overwritten without any of its old members being destroyed, which could cause memory/resource leaks.


Revision [16617]

Edited on 2013-03-03 21:00:33 by CountingPine ['Print ""' -> 'Print'; -Sleep; formatting]
Additions:
A copy ##**Constructor**## is a special constructor that initializes a new object from an existing object. There are three general cases where the copy ##**Constructor**## is called: when instantiating one object and initializing it with another object (in one instruction), when passing an object by value, when an object is returned from a function by value (by using ##[[KeyPgReturn Return]] //x//## statement).
Note: When an object is returned from a function by value, but by using ##**Function** = //x//## (or ##//function_identifier// = //x//##) assignment, the ##**Constructor**## is called once at first, and then the ##[[KeyPgOpLet Let (Assign)]]## operator at each assignment.
Deletions:
A copy ##**Constructor**## is a special constructor that initializes a new object from an existing object. There are three general cases where the copy ##**Constructor**## is called: when instantiating one object and initializing it with another object (in one instruction), when passing an object by value, when an object is returned from a function by value (by using ##[[KeyPgReturn Return]]## x statement).
Note: When an object is returned from a function by value, but by using Function = x (or function-identifier = x) assignment, the ##**Constructor**## is called once at first, and then the ##[[KeyPgOpLet Let (Assign)]]## operator at each assignment.


Revision [16616]

Edited on 2013-03-02 16:15:12 by FxMwikki [(copy) Constructor and Let operator callings depending on the 3 methods of returning from a function]
Additions:
A copy ##**Constructor**## is a special constructor that initializes a new object from an existing object. There are three general cases where the copy ##**Constructor**## is called: when instantiating one object and initializing it with another object (in one instruction), when passing an object by value, when an object is returned from a function by value (by using ##[[KeyPgReturn Return]]## x statement).
Note: When an object is returned from a function by value, but by using Function = x (or function-identifier = x) assignment, the ##**Constructor**## is called once at first, and then the ##[[KeyPgOpLet Let (Assign)]]## operator at each assignment.
Deletions:
A copy ##**Constructor**## is a special constructor that initializes a new object from an existing object. There are three general cases where the copy ##**Constructor**## is called: when instantiating one object and initializing it with another object (in one instruction), when passing an object by value, when an object is returned from a function (by value).


Revision [16530]

Edited on 2013-01-04 07:51:02 by FxMwikki [Added text explaining when and how use the copy constructor, with a consistent example]
Additions:
A copy ##**Constructor**## must be defined if the shallow implicit copy constructor is not sufficient. This happens in cases when the object manages dynamically allocated memory or other resources which need to be specially constructed or copied (for example if a member pointer points to dynamically allocated memory, the implicit copy constructor will simply do an implicit pointer construction and a copy of value instead of allocate memory and then perform the copy of data).
Deletions:
A copy ##**Constructor**## must be defined if the shallow implicit copy constructor is not sufficient. This happens in cases when the object manages dynamically allocated memory or other resources which need to be specially constructed and copied (for example if a member pointer points to dynamically allocated memory, the implicit copy constructor will simply do an implicit pointer construction and a copy of value instead of allocate memory and then perform the copy of data).


Revision [16529]

Edited on 2013-01-04 06:39:29 by FxMwikki [Added text explaining when and how use the copy constructor, with a consistent example]
Additions:
A copy ##**Constructor**## is a special constructor that initializes a new object from an existing object. There are three general cases where the copy ##**Constructor**## is called: when instantiating one object and initializing it with another object (in one instruction), when passing an object by value, when an object is returned from a function (by value).
Deletions:
A copy ##**Constructor**## is a special constructor that initializes a new object from an existing object. There are three general cases where the copy ##**Constructor**## is called: when instantiating one object and initializing it with another object, when passing an object by value, when an object is returned from a function (by value).


Revision [16528]

Edited on 2013-01-04 06:05:04 by FxMwikki [Added text explaining when and how use the copy constructor, with a consistent example]
Additions:
A copy ##**Constructor**## is a special constructor that initializes a new object from an existing object. There are three general cases where the copy ##**Constructor**## is called: when instantiating one object and initializing it with another object, when passing an object by value, when an object is returned from a function (by value).
A copy ##**Constructor**## must be defined if the shallow implicit copy constructor is not sufficient. This happens in cases when the object manages dynamically allocated memory or other resources which need to be specially constructed and copied (for example if a member pointer points to dynamically allocated memory, the implicit copy constructor will simply do an implicit pointer construction and a copy of value instead of allocate memory and then perform the copy of data).
Note: Even if is defined an explicit default ##**Constructor**##, it is never called by the implicit copy constructor.
Example of copy constructor.
{{fbdoc item="filename" value="examples/manual/udt/copyconstructor.bas"}}%%(freebasic)
Type UDT
Dim As String Ptr p ''pointer to string
Declare Constructor () ''default constructor
Declare Constructor (Byref rhs As UDT) ''copy constructor
Declare Destructor () ''destructor
Constructor UDT ()
This.p = Callocate(1, Sizeof(String))
Constructor UDT (Byref rhs As UDT)
This.p = Callocate(1, Sizeof(String))
*This.p = *rhs.p
Destructor UDT ()
*This.p = ""
Deallocate This.p
Dim As UDT u0
*u0.p = "copy constructor exists"
Dim As UDT u = u0
*u0.p = "" ''to check the independance of the result copy with the object copied
Print *u.p


Revision [16174]

Edited on 2012-05-15 10:55:47 by CountingPine [Rewrite note]
Additions:
##**Constructor**## can be also called directly from the ##//typename//## instance like the other member methods (##[[KeyPgSub Sub]]##) and with the same syntax, i.e. using a member access operator, e.g. ##//obj//.**Constructor**(//parameters//)##. No ##[[KeyPgDestructor destructor]]## is called, and the old object state - if any - is overwritten without any of its old members being destroyed, which could cause memory/resource leaks.
Deletions:
//Note: Constructor can be also called directly from the typename instance, as the other member methods (Sub) and with the same syntax (using a member access operator). The object instance, already existing (no new memory allocation), is just reconstructed (warning for member string: the memory allocated to the previous character data is not freed when the descriptor is cleared).//


Revision [16171]

Edited on 2012-05-09 06:16:42 by FxMwikki ['instance.constructor()' does not free the character data memory allocated to member string]
Additions:
//Note: Constructor can be also called directly from the typename instance, as the other member methods (Sub) and with the same syntax (using a member access operator). The object instance, already existing (no new memory allocation), is just reconstructed (warning for member string: the memory allocated to the previous character data is not freed when the descriptor is cleared).//
Deletions:
//Note: Constructor can be also called directly from the ##typename## instance, as the other member methods (Sub) and with the same syntax (using a member access operator). The object instance, already existing (no new memory allocation), is just reconstructed (warning for member string: the memory allocated to the previous character data is not freed).//


Revision [16169]

Edited on 2012-05-08 04:15:24 by FxMwikki ['instance.constructor()' does not free the character data memory allocated to member string]
Additions:
//Note: Constructor can be also called directly from the ##typename## instance, as the other member methods (Sub) and with the same syntax (using a member access operator). The object instance, already existing (no new memory allocation), is just reconstructed (warning for member string: the memory allocated to the previous character data is not freed).//
Deletions:
//Note: Constructor can be also called directly from the ##typename## instance, as the other member methods (Sub) and with the same syntax (using a member access operator). The object instance, already existing (no new memory allocation), is just reconstructed (warning for member string: the memory allocated for the previous character data is not freed).//


Revision [16168]

Edited on 2012-05-08 04:13:55 by FxMwikki ['instance.constructor()' does not free the character data memory allocated to member string]
Additions:
//Note: Constructor can be also called directly from the ##typename## instance, as the other member methods (Sub) and with the same syntax (using a member access operator). The object instance, already existing (no new memory allocation), is just reconstructed (warning for member string: the memory allocated for the previous character data is not freed).//
Deletions:
//Note: Constructor can be also called directly from the ##typename## instance, as the other member methods (Sub) and with the same syntax (using a member access operator). The object instance, already existing (no new memory allocation), is just reconstructed.//


Revision [16080]

Edited on 2012-02-05 08:06:40 by FxMwikki [Bad link to 'Class' in paragraph 'See also']
Additions:
- ##[[KeyPgClass Class]]##
Deletions:
- ##[[KeyPgType Class]]##


Revision [15838]

Edited on 2012-01-25 11:48:05 by CountingPine [of->or]
Additions:
name of the ##[[KeyPgType Type]]## or ##[[KeyPgClass Class]]##
Deletions:
name of the ##[[KeyPgType Type]]## of ##[[KeyPgClass Class]]##


Revision [15597]

Edited on 2011-12-31 06:14:26 by FxMwikki [Complement about constructor called as a member method.]
Additions:
//Note: Constructor can be also called directly from the ##typename## instance, as the other member methods (Sub) and with the same syntax (using a member access operator). The object instance, already existing (no new memory allocation), is just reconstructed.//
Deletions:
//Note: Constructor can be also called directly from the ##typename## instance, as for the other member methods and with the same syntax (using a member access operator). The object instance, already existing (no new memory allocation), is just reconstructed.//


Revision [15595]

Edited on 2011-12-29 16:15:09 by FxMwikki [Complement about constructor called as a member method.]
Additions:
//Note: Constructor can be also called directly from the ##typename## instance, as for the other member methods and with the same syntax (using a member access operator). The object instance, already existing (no new memory allocation), is just reconstructed.//
Deletions:
//Note: Constructor can be also called directly from the ##//typename//## instance, as for the other member methods and with the same syntax (using a member access operator). The object instance, already existing (no new memory allocation), is just reconstructed.//


Revision [15593]

Edited on 2011-12-29 16:09:18 by FxMwikki [Complement about constructor called as a member method.]
Additions:
//Note: Constructor can be also called directly from the ##//typename//## instance, as for the other member methods and with the same syntax (using a member access operator). The object instance, already existing (no new memory allocation), is just reconstructed.//
Deletions:
//Note: Constructor can be also called directly from the ##//typename//## instance, as for the other member methods and with the same syntax (using a member access operator). The object instance, already existing (no new memory allocation), is just reconstructed.//


Revision [15592]

Edited on 2011-12-29 16:05:58 by FxMwikki [Complement about constructor called as a member method.]
Additions:
Constructors are called when declaring global or local static instances of ##//typename//## and when allocating ##//typename//## dynamically using the ##[[KeyPgOpNew New]]## operator. See examples below for different constructor syntaxes.
//Note: Constructor can be also called directly from the ##//typename//## instance, as for the other member methods and with the same syntax (using a member access operator). The object instance, already existing (no new memory allocation), is just reconstructed.//
Deletions:
Constructors are called when declaring global or local static instances of ##//typename//## and when allocating ##//typename//## dynamically using the ##[[KeyPgOpNew New]]## operator.


Revision [14281]

Edited on 2009-08-29 21:52:00 by CountingPine [Remove CamelCase links in "New to FreeBASIC"]
Additions:
- New to ""FreeBASIC""
Deletions:
- New to FreeBASIC


Revision [12247]

The oldest known version of this page was created on 2008-01-18 14:08:54 by JeffMarshall [Remove CamelCase links in "New to FreeBASIC"]
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki



sf.net phatcode