Are Objects pointers?
BlitzMax Forums/BlitzMax Beginners Area/Are Objects pointers?
| ||
Ok, so if I understand correctly, all objects are actually just pointers. Is this correct? If I have something like this:Local MyObject:TGameObject = New TGameObject Local MyObject2:TGameObject = MyObject MyObject2.X = 100 then does MyObject.X also equal one hundred, because both objects are pointing to the same data? I would test this myself right now but I'm writing this from my iPod. Thanks. :) |
| ||
does MyObject.X also equal one hundred Yes. They will both point to the same object in memory. |
| ||
Thank you. |
| ||
If you want to achieve the same thing with regular variables (integers, floats etc.) then you can use Var. |
| ||
On a slightly related note - had a minor "palm on forehead" moment earlier. Basically I wanted to switch some values contained within two types. I figured: local type1:myType, type2:myType '(just for example) Local temp:myType = type2 type2.imageframe = type1.imageframe type1.imageframe = temp.imageframe type myType field x:int field y:int field imageframe:int end typeOf course, rather than swapping the values, the above resulted in two types with the same imageframe, since temp was simply an instance of type2. Might be a dumb question but is there a way of creating a copy of an object, rather than an instance of an existing one? I'm sure there's a way but my brain is fried from coding magic spells all day (don't ask). |
| ||
I don't think there is... Even if there was it would basically be complicated... I mean the type instance could be copied but what would that get you.. what if there is a TImage.. should it copy the image or just copy the reference as an example... or worse.. if there is a type reference within the type does it copy the referenced type as well or retain a reference.. Ultimately you will likely have to make your own copy function anyway... |
| ||
Copy objects with reflection |