A field pointing to a variable

BlitzMax Forums/BlitzMax Programming/A field pointing to a variable

Hezkore(Posted 2011) [#1]
Is it possible to have a field point to a variable?
Type MyType
   Field MyVar Var
EndType
That does NOT work, but it's something like that I want.
Possible... yes... no?

Last edited 2011


Czar Flavius(Posted 2011) [#2]
What is the type of this field?

Types are stored by reference, so you can have multiple variables and therefore type fields that refer (point) to the same object.

Type Bob
	Field a:Int
End Type

Local b1:Bob = New Bob
Local b2:Bob = b1
b2.a = 5
Print b1.a


This prints 5. Changing b2 also affects b1 as they in fact refer to the same object.

So it is enough to make the field "="s another variable which refers to that object, and it will "point" to that object as well. This also works for arrays.

The exception is for basic data types such as int, float and also strings. These are actually copied with the = operation. So if this is the type you want a pointer to, it is considerably easier just to wrap up the variable in a type, which will automatically use the reference nature described above. For example,

Type TInt
	Field value:Int
	
	Function Create:TInt(v:Int)
		Local i:TInt = New TInt
		i.value = v
		Return i
	End Function
End Type



Hezkore(Posted 2011) [#3]
Yes of course, but I want my field to be a pointer to a variable, the same "Var" as in a function.
Local a:int=2
Function ChangeVariable(a Var)
	a=10
End Function
ChangeVariable(a)
Print(a)
That would print "10" as I've pointed a variable to my function, thanks to "Var".
In the same way I would like to have a field point to a variable.

Last edited 2011


Tommo(Posted 2011) [#4]
Just use pointer.
Type
   Field target:Int Ptr
   Method Change(val:Int)
     target[0]=val
   End Method
   
   Method SetTarget(variable:Int Var)
     target=Varptr variable
   End Method
End Type



Hezkore(Posted 2011) [#5]
How do I get the value from a Int Ptr?
Printing my IntPointer only gives me
Compile Error: Unable to convert from 'Int Ptr' to 'String'
Same if I try to convert it to a string by using String() and if I use Int() it gives me some random high number (Guess it's the pointer address?)

Also, is there a String Pointer?

UPDATED!
Nevermind, I should have looked closer on your example! x)
I should have used MyIntPtr[0] instead of trying to convert it to String or Int!
Still, is there are String Pointer?

Last edited 2011


HrdNutz(Posted 2011) [#6]
I think pointers to primitive datatypes (int float double) are like array pointers, thus you access the data by addressing specific element.
local i:Int = 10  ' create integer variable and assign 10 as value
local ptr:Int Ptr = Varptr(i)  ' create int pointer and assign variable's address, now ptr points to the same data as i variable.
local j:int = ptr[0]  ' to access the data by the pointer, you retrieve it like you access an array,
so for a single integer it's the first element [0],
if you made an Int Ptr to an array of integers, you could access any element [0-len-1]


Strings are immutable objects in bmax, meaning they are pointers to begin with. If you pass a string to a function - it's a reference being passed. Immutable means that any operation you do with a string will result in a brand new string object created, you cannot modify existing strings, only generate new strings. So if you want to modify a string inside a function without returning a new string, just pass it by value as variable and not pointer.
local str:String = "immutable"
aug(str)
print str
' Var denotes the variable as by value and not by reference, thus any 
' modifications applied here will affect the outside variable as well.
function aug(s:String Var) 
  s = s + " augment"  ' 
end function


Last edited 2011