No pointers to objects!?

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

Ryan Burnside(Posted 2008) [#1]
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...


Sledge(Posted 2008) [#2]
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.


Ryan Burnside(Posted 2008) [#3]
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.


Sledge(Posted 2008) [#4]
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.


Kurator(Posted 2008) [#5]
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.


Grey Alien(Posted 2008) [#6]
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.


Sledge(Posted 2008) [#7]
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.




Yan(Posted 2008) [#8]
...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.