To select elements in an object
obj you use
name is one of the element names.
In the example, the
tag element of the
a is selected by writing
The other elements are selected in a similar way.
Just like an array declaration the address of an object
obj is stored in the variable
obj, and any pointer of type
PTR TO objectname can be used just like an object of type objectname.
Therefore, in the previous example
a is a
PTR TO rec.
As the example object shows, the elements of an object can have several different types. In fact, the elements can have any type, including object, pointer to object and array of object. The following example shows how to access some different typed elements.
OBJECT rec tag, check table:ARRAY data:LONG ENDOBJECT OBJECT bigrec data:PTR TO LONG subrec:PTR TO rec rectable:ARRAY OF rec ENDOBJECT PROC main() DEF r:rec, b:bigrec, rt:PTR TO rec r.table:="H" b.subrec:=r b.subrec.tag:=1 b.subrec.data:=r.tag+(10000*b.subrec.tag) b.subrec.table:="i" b.rectable.data:=r.tag b.rectable.table:="A" rt:=b.rectable rt.data++:=0 rt.table--:="B" ENDPROC
- operators apply to first thing in the selection (i.e.,
rt in both the last two assignments in the example above), and may occur only after all the selections.
Notice that object selection and array indexing can be repeated as much as necessary (but only as the types of the elements allow).
As a simple example, consider the third assignment:
This selects the
subrec element from the
b, and then sets the
tag element of this
rec object to 1.
Now, consider one of the later assignments:
This selects the
rectable element from
b, which is an array of
The first element of this array is selected, and then the
table element of the
rec object is selected.
Finally, the first character of the
table is set to the character A.
As you can probably tell, it is important to give the elements of objects appropriate types if you want to do multiple selection in this way. However, this is not always possible or the best way of doing some things, so there is a way of giving a different type to pointers (this is called explicit pointer typing--see the Reference Manual for more details).
Here's a quite simple example which uses an array of objects:
OBJECT rec tag, check table:ARRAY data:LONG ENDOBJECT PROC main() DEF a:ARRAY OF rec, p:PTR TO rec, i p:=a FOR i:=0 TO 9 a[i].tag:=i p.check++:=i ENDFOR FOR i:=0 TO 9 IF a[i].tag<>a[i].check WriteF('Whoops, a[\d] went wrong...\n', i) ENDIF ENDFOR ENDPROC
If you think about it for long enough you'll see that
a.tag is the same as
a is a pointer to the first element of the array, and the elements of the array are objects.
a is a pointer to an object (the first object in the array).
Go to the Next or Previous section, the Detailed Contents, or the Amiga E Encyclopedia.