Shop OBEX P1 Docs P2 Docs Learn Events
Array of objects?? — Parallax Forums

Array of objects??

RogerInHawaiiRogerInHawaii Posts: 87
edited 2009-02-24 02:46 in Propeller 1
I'm writing some Spin code. What I'd really like is an array of C++ -like objects.

That is I'd like to have each element within the array to consist of two or more items, say a byte value, a long value, and a word value (or even something more complex). But there doesn't seem to be any way to specify such an array in Spin.

The best I've come up with is something like:

[noparse][[/noparse]CODE]
VAR

··BYTE MyByte0
··LONG MyLong0
· WORD MyWord0

· BYTE MyByte1
··LONG MyLong1
··WORD MyWord1

· BYTE MyByte2
··LONG MyLong2
··WORD MyWord2

· LONG MyArray[noparse][[/noparse]3]

PUB Main

· ' Initialize the array elements to point at the "objects"
· MyArray[noparse][[/noparse]0] := @@MyByte0
· MyArray[noparse][[/noparse]1] := @@MyByte1
· MyArray[noparse][[/noparse]0] := @@MyByte2
[noparse][[/noparse]/CODE]

And then I can access MyArray[noparse][[/noparse]i] to get a pointer to the desired "object" and from there access
the individual elements of the "object".

Is that a reasonable way to do it? Is there a better way?

Ideally I'd like to be able to very simply specify at compile time how many·"objects" are in the array without havingto explicity·specify each individual object and explicitly·initialize the pointers. That is, ideally I'd like something more akin to C++ style objects. I know that's asking a lot from Spin. [noparse]:)[/noparse]


PS: What happened to the CODE designators in the New Post window? They're gone, and it doesn't seem to recognize when I excplicitly enter them myself.

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2009-02-24 02:46
    What you've written won't work because the compiler allocates longs first, then words, then bytes in order to save space. What you can do is to declare an array of longs and access pieces of it using the ".byte" and ".word" suffixes as needed. You could have a named constant for the number of objects and allocate the long array using that. It's not nice and clean as it would be in C++, but it can sort-of be done.
Sign In or Register to comment.