Code: Select all
operator TForm1.let (tt as TForm1)
' FileType = tt.FileType
thisp_ = @this
end operator
Code: Select all
operator TForm1.let (tt as TForm1)
' FileType = tt.FileType
thisp_ = @this
end operator
OK, thanks.fxm wrote:The topic (viewtopic.php?f=17&t=20434) is not deleted.
I only deleted my last today post which was in fact erroneous.
Code: Select all
type TExample extends object
text as string
declare constructor()
end type
constructor TExample()
end constructor
function Test(number as integer) byref as TExample
static example as TExample
' clear object (uncomment to see difference)
' example = TExample
if number > 0 then
example.text = "positive"
elseif number < 0 then
example.text = "negative"
end if
return example
end function
dim example as TExample
example = Test(3)
print example.text
example = Test(-2)
print example.text
example = Test(0)
print example.text
end
Code: Select all
type TExample extends object
text1 as string
text2 as string
declare constructor()
declare constructor(value as string)
end type
constructor TExample()
text1 = "positive"
end constructor
constructor TExample(value as string)
inherited 'pseudo code invokes default constructor
text2 = value
end constructor
dim example as TExample = TExample("negative")
print example.text1
print example.text2
end
Code: Select all
type TExample extends object
text1 as string
text2 as string
declare constructor()
declare constructor(value as string)
end type
constructor TExample()
text1 = "positive"
end constructor
constructor TExample(value as string)
constructor() 'to be always used at the first line 'inherited 'pseudo code invokes default constructor
text2 = value
end constructor
dim example as TExample = TExample("negative")
print example.text1
print example.text2
sleep
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).
Code: Select all
Type test
As Integer x, y
Declare Destructor
End Type
Destructor test()
? "Destroy: "; Hex(@this)
End Destructor
Dim As test Ptr p = @(type<test>(12,13))
? Hex(p), p->x, p->y
Sleep
Code: Select all
Type test
As Integer x, y
As String s
Declare Destructor
Declare Constructor ()
Declare Constructor (Byval x As Integer, Byval y As Integer, Byref s As String)
End Type
Destructor test()
? "Destroy: "; Hex(@this)
End Destructor
Constructor test ()
End Constructor
Constructor test (Byval x As Integer, Byval y As Integer, Byref s As String)
This.x = x
This.y = y
This.s = s
End Constructor
Dim As test Ptr p = @test(12, 13, "string")
? Hex(p), p->x, p->y, "'" & p->s & "'"
Sleep
Code: Select all
Destroy: 19FE8C
19FE8C 12 13 ''
Code: Select all
type test
x as integer
y as integer
s as string
static pthis as test ptr
declare destructor()
declare constructor()
declare constructor(byval x as integer, byval y as integer, byref s as string)
end type
constructor test()
pthis = @this
print "assigned static pointer in default constructor:"; hex(pthis)
end constructor
constructor test(byval x as integer, byval y as integer, byref s as string)
constructor()
this.x = x
this.y = y
this.s = s
end constructor
destructor test()
print "destroy: "; hex(@this)
end destructor
dim test.pthis as test ptr
print "instantiating: t as test = test(12, 13, 'string')"
dim t as test = test(12, 13, "string")
print "member values:"
print t.x, t.y, "'" & t.s & "'"
print "pointer:"; hex(@t)
print "static pointer:"; hex(t.pthis)
print
print "instantiating: t=test:"
t = test()
print "member values:"
print t.x, t.y, "'" & t.s & "'"
print "pointer:"; hex(@t)
print "static pointer:"; hex(t.pthis)
end
Code: Select all
type test
x as integer
y as integer
s as string
static pthis as test ptr
declare destructor()
declare constructor()
declare constructor(byval x as integer, byval y as integer, byref s as string)
declare operator let(tt as test)
end type
constructor test()
pthis = @this
print "assigned static pointer in default constructor:"; hex(pthis)
end constructor
constructor test(byval x as integer, byval y as integer, byref s as string)
constructor()
this.x = x
this.y = y
this.s = s
end constructor
destructor test()
print "destroy: "; hex(@this)
end destructor
operator test.let(tt as test)
pthis = @this
print "assigned static pointer in operator:"; hex(pthis)
end operator
dim test.pthis as test ptr
print "instantiating: t as test = test(12, 13, 'string')"
dim t as test = test(12, 13, "string")
print "member values:"
print t.x, t.y, "'" & t.s & "'"
print "pointer:"; hex(@t)
print "static pointer:"; hex(t.pthis)
print
print "instantiating: t=test:"
t = test()
print "member values:"
print t.x, t.y, "'" & t.s & "'"
print "pointer:"; hex(@t)
print "static pointer:"; hex(t.pthis)
end
That's not for a Type. That's for an object, which also (still) uses the Type keyword. Hopefully, one day with full class implementation (if that is ever to happen), the difference and behavior between the two will be more logical. Currently FreeBasic appears to be in sort of an in-between state. The examples above demonstrate unpreferred behavior with objects IMHO, which may or may not be the result of the TYPE legacy. I can't tell.fxm wrote:More generally, for a Type containing at least a pointer as member (and therefore potentially dynamic data), user must always ask himself if the implicit copy-constructor, implicit assignment-operator and implicit destructor are sufficient or must be redefined.