MrSwiss wrote:VANYA wrote:I understand that, but I'm interested in this question which basically it turns the extra destructor is called
Since the temporary 'type' (which takes the data from the locally created/destroyed 'type')
is constructed at 'return time', it can only be destroyed in MAIN-code ...
(in the Function you are constructing twice, but destroying once only)
In other words: there is an equal amout of ctor/dtor activity, meaning all is OK!
(there is nothing "extra" even if it seems that way at first)
I think we are talking about different things. I'm asking:
If the programmer determines its own constructor in the class , why is called the implicit constructor\\destructor?
Look, in the following code already inserted the implicit constructor (that is, make it explicit):
Code: Select all
Type rational
numerator As Integer
denominator As Integer
Declare Constructor()
declare Constructor (p as rational)
Declare Destructor()
End Type
Constructor rational()
Print "*created: "; @This
End Constructor
Constructor rational(p as rational)
Print "*created: "; @This
End Constructor
Destructor rational()
Print "destroyed ";@This
End Destructor
Function rational_multiply( ) As rational
Dim r As rational
Return r
End Function
rational_multiply()
sleep
and the result is as expected (2 ctor + 2 dtor):
*created: 140737405912352
*created: 140737405912416
destroyed 140737405912352
destroyed 140737405912416
But if we do not specify explicitly:
Constructor rational(p as rational)
Code: Select all
Type rational
numerator As Integer
denominator As Integer
Declare Constructor()
Declare Destructor()
End Type
Constructor rational()
Print "*created: "; @This
End Constructor
Destructor rational()
Print "destroyed ";@This
End Destructor
Function rational_multiply( ) As rational
Dim r As rational
Return r
End Function
rational_multiply()
sleep
the result:
*created: 140720577747840
destroyed 140720577747840
destroyed 140720577747904
(1 ctor + 2 dtor)
Why , if we do not specify in code the
Constructor rational(p as rational) , its call, as well as the call to Destructor, is in the final program? Isn’t it logical to disable the call to the implicit constructor at the compiler level with an existing custom constructor?