Why code with the operator . (dot) does not behave as I expect this code:
Code: Select all
type AAA
as Byte M(10)
End Type
type CLASSBB
vData as AAA ptr ptr
iSize as Long
iRealMemSize as Long
declare function push_back(vData as AAA ptr) as BOOLEAN
Declare Operator [] (byref iIndex as Long) byref As AAA ptr
Declare Operator let (byref v as AAA ptr)
End Type
function CLASSBB.push_back(vData as AAA ptr) as BOOLEAN
if this.iSize >= this.iRealMemSize then
this.iRealMemSize +=10
this.vData =reallocate(this.vData , (this.iRealMemSize)* sizeof(AAA ptr))
if this.vData =0 then
return FALSE
EndIf
EndIf
(this.vData)[iSize] =vData
iSize +=1
return TRUE
End Function
Operator CLASSBB.[] (byref iIndex as Long) byref As AAA ptr
return (this.vData)[iIndex]
End Operator
function alloc() as AAA ptr
dim p as AAA ptr = callocate(sizeof(AAA))
? "real P=", p
return p
End function
'example (wrong result)
dim v as CLASSBB
dim as AAA ptr p = alloc()
v.push_back(p) ' first pointers
v[0].M(0) = 1
p = alloc()
v.push_back(p) ' second pointers
v[0].M(0) = 2
' print ponters
for i as Long = 0 to v.iSize-1
? V[i]
Next
sleep
RESULT:
However , if you replace the line with the operator . (dot)'real P= 25522560 <-
'real P= 25527680
'25522434 <-
'25527680
Code: Select all
v[0].M(0) = 2
Code: Select all
pTemp = v[0]
pTemp->M(0) = 2
then everything is fine:
Code: Select all
type AAA
as Byte M(10)
End Type
type CLASSBB
vData as AAA ptr ptr
iSize as Long
iRealMemSize as Long
declare function push_back(vData as AAA ptr) as BOOLEAN
Declare Operator [] (byref iIndex as Long) byref As AAA ptr
Declare Operator let (byref v as AAA ptr)
End Type
function CLASSBB.push_back(vData as AAA ptr) as BOOLEAN
if this.iSize >= this.iRealMemSize then
this.iRealMemSize +=10
this.vData =reallocate(this.vData , (this.iRealMemSize)* sizeof(AAA ptr))
if this.vData =0 then
return FALSE
EndIf
EndIf
(this.vData)[iSize] =vData
iSize +=1
return TRUE
End Function
Operator CLASSBB.[] (byref iIndex as Long) byref As AAA ptr
return (this.vData)[iIndex]
End Operator
function alloc() as AAA ptr
dim p as AAA ptr = callocate(sizeof(AAA))
? "real P=", p
return p
End function
'example (good result)
dim v as CLASSBB
dim as AAA ptr p = alloc()
v.push_back(p) ' first pointers
dim as AAA ptr pTemp = v[0]
pTemp->M(0) = 1
p = alloc()
v.push_back(p) ' second pointers
pTemp = v[0]
pTemp->M(0) = 2
' print ponters
for i as Long = 0 to v.iSize-1
? V[i]
Next
sleep
P.S. Linux 64 , last compiler'real P= 10838400
'real P= 10843520
'10838400
'10843520