Chapter 3 The Application Description Language

3.16 Object Definition

Defining an instance of an object is similar to defining a variable, but the initialization of objects is more complex. We deal with this topic more fully in Section 3.25, "Object Initialization" page 49, where we discuss the process of initializing objects. In this section we are concerned only with the syntax for defining objects.

The default object declaration takes the form of a class name followed by a non-null list of object identifiers. More generally, an object declarator substitutes for each identifier. An object declarator is an identifier combined with two optional constructions that specify how the object is to be initialized.
Object Declaration
Simple Object Declaration
MyClass myObj1, myObj2;
Object Declaration with Custom Constructor
MyClass { 'CustomConstruct, arg1, ..., argn} => myObj;
Object Declaration with Initializor Block
MyClass myObj1 {

height=100;width=100;

};

Object Declaration with Special Constructor and Initializor Block
MyClass { 'CustomConstruct, arg1, ..., argn} => myObj1 {

height=100;width=100;

};

The simple form of object declaration shown in the first example of Figure 3.33 would create two instances of the MyClass named myObj1 and myObj2 respectively. When these two instances are created, the standard message Construct is, by default, sent to each instance. The more complicated example shown in that same figure illustrates how the default Construct message can be over-ridden. In this example, the message

{'CustomConstruct, arg1, ... argn}

is sent when the instance myObj is created instead of the simple Construct message. This requires, of course, that the class named MyClass have a constructor method named CustomConstruct and that the member and type of the arguments in the message agree with those specified for the method. A class can have any number of constructor methods, but only one of these can be used to create any given instance of the class. As discussed further in Section 3.25, "Object Initialization" page 49, constructor methods are distinguished fom ordinary methods in ADL by beginning them with the keyword upon rather than the keyword on.

The third example in Figure 3.34 illustrates the use of initializor blocks. These blocks consist of a series of ADL statements enclosed in curly brace delimiters. Initializor blocks allow an ADL programmer to set values for the attributes of an instance of a class being constructed. In the example in the figure, the attributes height and width are each initialized to 100. Initializor blocks (or izor blocks for short) are used very often by ADL programmers becasue they provide a very convenient way to set attribute values for newly-created objects. However, it is important to stress that the ADL statements in an izor block are executed as though they were part of the object being created, not as part of the object in which they actually appear. This is termed a foreign scope in ADL. This means that the right hand side of the assignment:

height 100

refers to the variable named height in the new instance of MyClass being created. The consequences of an izor blocks being executed in a foreign scope are often not obvious to beginning ADL programmers. For example, consider a fragment of ADL code as follows:

integer xvalue=50;    //declare an integer initialized to 50
MyClass myObj {height=zvalue;}; //incorrect use of izor block

One migh incorrectly expect that the value of the attribute named height in MyClass would be set to 50 by this code. Instead, this code will produce an error because the izor block that attempts to set height is executed in the scope of the class MyClass, and the variable xvalue on the right hand side of the assignment is not defined in this scope. Thecorrect way to achieve the desired result in ADL would be using the following cde fragment:

integer xvalue=50;  //declares an integer initialized to 50
MyClass my Ob;      //create instance of MyClass named myObj
myObj.height=value; //set the height attribute of myObj

The fourth example in Figure 3.34 combines the use of a custom constructor with the useo f an izor block. While the detals of the order in which things occur when a new object is constructed are covered in much more detail in Section 3.25, "Object Initialization" page 49, it is worth noting that the execution f the custom constructor occurs before the execution of the izor block. Thus, any statements in the constructor method that set values of attributes that are also set in the izor block will be over-ridden.

An object declaration can contain multiple object declarators, each with its own identifier, just as it can contain multiple simple identifiers. Object instances are not variables and cannot receive assignments.[9] Objects declared within a scope are destroyed upon exit from that scope. Never destroy declared objects using the delete operator. In defining an object, you create a new instance of the wrapped C++ class, initialize it, and make it visible to the ADL in the current scope if the object is an instance of a system-defined wrapped class. If the object is an instance of a user-defined ADL class, then you create a new instance of that class along with all its members and bases (see Section 3.23, "Wrapped Classes" page 44 for more information on wrapped, or system defined, classes and user-defined classes).


[9] This avoids forcing the user to confront the complexity of the C++ copy constructor. Default cloning is provided (see Section 3.17, "Dynamic Objects and Storage Management" page 36).
AM2 Documentation - 19 NOV 1996

Generated with Harlequin WebMaker