|
| Previous: 2.9.2 Classes | TOC | Index | Back | Next: 2.9.4 Modules and Class Dependencies |
Objects are used to instantiate classes. An OBJECT statement is to a CLASS definition what a VAR statement is to a STRUCT definition. While the class defines the layout of an object, the OBJECT statement actually creates an object in memory. The general form of the object definitition is as follows:
OBJECT anObject[aClass], ... ;
Any number of objects may be defined in a single OBJECT statement. Each class may be instantiated any number of times and different classes may be instantiated in the same statememt. The name of the object to define is specified before the square brackets and the class of the object inside of the brackets:
OBJECT str[string];
creates an object of the class string named str.
An object may be a factor in an expression. It evaluates to the address of its first member. The notations
objectname
and
@objectname
are equivalent.
When creating multiple instances of a class, only the data defintions of the class are instantiated. The methods of a class belong to the class rather than the object. They are created when a class is declared.
The only way to alter the state of an object is to send it a message. Therefore, the state of each object is - in the ideal case - completely independent from the states of other objects, even if they belong to the same class. In a hybrid language like T3X, however, the procedures of a class may change data objects defined in the global scope. Changing a global object from within an object changes the state of all other objects of the same class (and maybe even others). Therefore, this technique is deprecated. Of course, there are situations where an object has to change the global state, for example when performing input/output operations. Classes defining such objects are said to have side effects.
| Previous: 2.9.2 Classes | TOC | Index | Back | Next: 2.9.4 Modules and Class Dependencies |