Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

ask about how object assigns work.


  • Please log in to reply
1 reply to this topic
szujeq
  • Members
  • 304 posts
  • Last active: Jan 12 2017 09:11 PM
  • Joined: 28 Mar 2011

When i assign some object to the other then i modifi once of them then any changes are visible in both object

object1.something := Object(some object content)
object2.something := object1.something
object1.something.item := "some value"
; now object1.something.item = object2.something.item

But now instead of modyfi I clear once then second is still exist

object1.something := Object(some object content)
object2.something := object1.something
object1.something := {} ; clearing
; now object2.something is still exist while i clear object1.something
; otherword it looklike
; object2.something := object1.something.Clone()
; object1.something := {} ; clearing

So why in 1st case any changes are apply to object1 and object2 while in 2nd case clearing refers to only object1?



kon
  • Members
  • 1652 posts
  • Last active:
  • Joined: 04 Mar 2013

object1 and object2 are not separate objects. They aren't even objects...

 

object1 and object2 are both variables that refer to the same object.

; Store a value
Var1 := "abc"

; Store a reference to Var1. &Var1 is the memory address of Var1.
Var2 := &Var1
Var3 := Var2

; Both Var2 and Var3 refer to the same memory address
MsgBox, % StrGet(Var2) "/" StrGet(Var3)		; abc/abc

; Change the value of Var1
Var1 := 123

; Var2 and Var3 still refer to the same memory address, but the contents of the memory at the address has changed.
MsgBox, % StrGet(Var2) "/" StrGet(Var3)		; 123/123

; Clear Var2
Var2 := ""

; Var2 no longer contains the address of Var1, but Var3 still does.
MsgBox, % StrGet(Var2) "/" StrGet(Var3)		; /123