No pointers to objects!?

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

Hmm I was wanting to do some networks of connected objects and was shocked to find that Bmax uses no pointers for objects.

I wanted to have a sort of web where each object knows it's parent and each object knows children. In this way networks could be connected or merged together. Why does Bmax not support object pointers? Pointers are really one of the keystones of OOP...

Isn't what you are trying to achieve covered by being able to do stuff like the following?...
SuperStrict
Graphics 800,600

Type TThing
	Field nextTThing:TThing
	Field prevTTHing:TThing
	Field value:Int
End Type

Local thingA:TThing = New TTHing
Local thingB:TThing = New TThing
thingB.value = 123

thingA.nextTThing = thingB
DrawText thingA.nextTThing.value,10,10

Flip
WaitKey

That you can do such without seeing an * or -> is one of the nice things about the language, I think.

Well sort of. You see storing copies entire objects rather than simple pointers is not very memory efficient from what I understand. I have some rather large objects and to store 5 copies of each object in another object is really not the best option. Unless they are passed by reference.

They are -- two New statements, two objects:
SuperStrict
Graphics 800,600

Type TThing
	Field nextTThing:TThing
	Field prevTTHing:TThing
	Field value:Int
End Type

Local thingA:TThing = New TTHing
Local thingB:TThing = New TThing

thingA.nextTThing = thingB
thingB.value = 123
DrawText thingA.nextTThing.value,10,10
thingA.nextTThing.value = 321
DrawText thingB.value,10,30

Flip
WaitKey

Given what you can do with pretty much any order of language these days, I'd say that having to explicitly denote references is what makes C/C++ irrevocably mid-level whereas not having to is what defines BMax as high level (and readable). Be aware, though, that the circular references will befuddle the garbage collector so null/update the appropriate fields when you attempt to release one of those objects.

Ryan, nextTThing and prevTThing are Object References (very similar to the references used in JAVA, C# and C++) and not Copies of Objects.

References are much more save to use than pointers, but give you pretty much of all the fuctionality of pointers.

Yeah as the others have said BMax does have object pointers. Every time you say ThisThing: TThing = ThatThing you are making a pointer, not a copy.

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


...and was shocked to find that Bmax uses no pointers for objects.
I'd be shocked if anyone actually bothered to read the docs. The second and third lines of 'Language>Objects' reads...
Objects are handled by reference. This means that the actual memory used by an object to store its data is shared between all the variables that refer to that object.

This makes objects very efficient to deal with, as assigning an object to a variable simply involves assigning a reference to the object - not an entire object. Passing objects to functions and returning objects from functions is also very efficient as again only references need to be copied.