Go to the Next or Previous section, the Detailed Contents, or the Amiga E Encyclopedia.


17.2 Objects in E

Classes are defined using OBJECT in the same way that we've seen before (see 9.4 OBJECT Type). So, in E, the terms `object declaration' and `class' may be used interchangeably. However, referring to an OBJECT type as a `class' signals the presence of methods in an object.

The following example OBJECT is the basis of a set class, as described above (see 17.1.2 Example class). This set implementation is going to be quite simple and it will be limited to a maximum of 100 elements.

OBJECT set
  elts[100]:ARRAY OF LONG
  size
ENDOBJECT

Currently, the only way to allocate an OOP object is to use NEW with an appropriately typed pointer. The following sections of code all allocate memory for the data of set, but only the last one allocates an OOP set object. Each one may use and access the set data, but only the last one may call the methods of set.

  DEF s:set

  DEF s:PTR TO set
  s:=NewR(SIZEOF set)

  DEF s:PTR TO set
  s:=NEW s

OOP objects can, of course, be deallocated using END, in which case the destructor for the corresponding class is also called. Leaving an OOP object to be deallocated automatically at the end of the program is not quite as safe as normal, since in this case the destructor will not be called. Also, when using END to deallocate an object you do not need to use a pointer of exactly the same type as the object (like you would for normal NEW allocations). Instead you can use a pointer of any of the base classes' types. Constructors and destructors are described in more detail below.


Go to the Next or Previous section, the Detailed Contents, or the Amiga E Encyclopedia.