No pointers to objects!?

BlitzMax Forums/BlitzMax Beginners Area/No pointers to objects!?

Following on from potential GC issues: Note also that you have a TList class and that you can maintain a list as a (static) class variable and that there are methods in the associated TLink class to find the next and previous entries in the list. Hence you don't necessarily need to maintain next and previous references as fields.

SuperStrict
Graphics 800,600

Type TThing
	Global list:TList = CreateList() ' Global denotes class variable (would be Static in C++).
	
	Field contents:Int
	
	Function Make:TThing(contents_arg:Int)
		Local newTThing:TThing = New TTHing
		newTThing.contents = contents_arg
		ListAddLast list,newTThing
		
		Return newTThing 
	End Function
End Type


For Local numberOfThings:Byte = 1 To 10
	Local newThing:TThing = TThing.Make(Rand(1,100)) 
Next

Local extraThing:TThing = TThing.Make(Rand(1,100))
extraThing = Null

'At this point we have eleven objects with references to each
'held solely in the list TThing.list

'Traverse list automatically:
If True ' Hello local scope
	Local yPos:Int = 10
	For Local iterator:TThing = EachIn TThing.list
		DrawText "Thing "+(yPos/10)+": "+iterator.contents,10,yPos
		yPos:+10
	Next
EndIf


'Traverse list manually:
If True ' Hello local scope
	Local yPos:Int = 10
	Local iterator:TThing = TThing( TThing.list.FirstLink().Value() )
	Repeat
		DrawText "Thing "+(yPos/10)+": "+iterator.contents,10,yPos+200
		If ( TThing.list.FindLink(iterator) ).NextLink() = Null
			Exit
		Else	
			iterator = TThing( ( TThing.list.FindLink(iterator) ).NextLink().Value() )
			yPos:+10
		EndIf
	Forever
EndIf

Flip
WaitKey