14.4 Objects


14.4 Objects

Remember, a class definition is just a type. Therefore, when you declare a class type you haven't created a variable whose fields you can manipulate. An object is an instance of a class; that is, an object is a variable that is some class type. You declare objects (i.e., class variables) the same way you declare other variables: in a var, static, or storage section.[4] Here is a pair of sample object declarations:

 var      T1: TypicalClass;      T2: TypicalClass; 

For a given class object, HLA allocates storage for each variable appearing in the var section of the class declaration. If you have two objects, T1 and T2, of type TypicalClass then T1.TCvar is unique as is T2.TCvar. This is the intuitive result (similar to record declarations); most data fields you define in a class will appear in the var declaration section of the class.

Static data objects (e.g., those you declare in the static or storage sections of a class declaration) are not unique among the objects of that class; that is, HLA allocates only a single static variable that all variables of that class share. For example, consider the following (partial) class declaration and object declarations:

 type      sc: class           var                i:int32;           static                s:int32;                .                .                .           endclass; var      s1: sc;      s2: sc; 

In this example, s1.i and s2.i are different variables. However, s1.s and s2.s are aliases of one another. Therefore, an instruction like "mov( 5, s1.s);" also stores five into s2.s. Generally you use static class variables to maintain information about the whole class while you use class var objects to maintain information about the specific object. Because keeping track of class information is relatively rare, you will probably declare most class data fields in a var section.

You can also create dynamic instances of a class and refer to those dynamic objects via pointers. In fact, this is probably the most common form of object storage and access. The following code shows how to create pointers to objects and how you can dynamically allocate storage for an object:

 var      pSC: pointer to sc;           .           .           .      malloc( @size( sc ) );      mov( eax, pSC );           .           .           .      mov( pSC, ebx );      mov( (type sc [ebx]).i, eax ); 

Note the use of type coercion to cast the pointer in EBX as type sc.

[4]Technically, you could also declare an object in a readonly section, but HLA does not allow you to define class constants, so there is little utility in declaring class objects in the readonly section.




The Art of Assembly Language
The Art of Assembly Language
ISBN: 1593272073
EAN: 2147483647
Year: 2005
Pages: 246
Authors: Randall Hyde

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net