How to store an object in an array (or similar data structures)
Harry1
Posts: 29
I am trying to store a Point Object. Point is an object i created and takes two vars (xVal, yVal) as parameters. Is it possible this type of an object in an array or similar data structures? Any help will be appreciated [noparse]:)[/noparse]·
Comments
you could declare a point array as "VAR long pointArray[noparse][[/noparse]32]" and store items as "pointArray[noparse][[/noparse]z] := pointOf(x,y)" or reference them as "xValOf(pointArray[noparse][[/noparse]z])"
You can indeed have arrays of objects. But you cannot (at least not "easily") copy objects, create or use a pointer or
reference to an object (other than in a purely static manner), subclass objects, etc.
So you can do this:
obj
pointArray[noparse][[/noparse]100] : "point"
which creates an array of 100 "pointArray" objects. (You can only create, I think, a maximum of 255 total
subobjects + procedures in any one object, so that 100 cannot be 1000.)
But it is probably far far more efficient (if not clean) to simply disassociate the data from the methods, so your
"point" class would not actually have any data, and you would just use a memory area with two longs in it as
data, and call those subroutines passing the pointer to the memory for the data. That is, use a C-style design
rather than a C++-style design.
thank you!
So if you store a point as two words in a long, you can:
sub hypot(pt) | x, y
x := pt.word[noparse][[/noparse] 0 ]
y := pt.word[noparse][[/noparse] 1 ]
return ^^(x * x + y * y)
Post Edited (rokicki) : 1/5/2007 9:45:33 PM GMT
(point.spin)
where the data is *in* the object, you instead do what Mike Green suggested or something
similar, so
(point.spin)
So you simply separate the data and the procedures that operate on that data rather than having
them in the same "object".
This has the advantage of keeping the object implementation details and their management hidden from the user. Of course, it doesn't make the most efficient use of memory and lacks an object destructor. This could be be improved by implementing some sort of dynamic memory management scheme, wherein destroyed objects could be reused.
-Phil