Home | Categories | Alphabetical | Classes | All Contents | [ < ] | [ > ]

Class Structures


Object instance data is contained in named IDL structures. We will use the term class structure to refer to IDL structures containing object instance data.

Beyond the restriction that class structures must be named structures, there are no limits on what a class structure contains. Class structures can include data of any type or organization, including pointers and object references. When an object is created, the name of the class structure becomes the name of the class itself, and thus serves to define the names of all methods associated with the class. For example, if we create the following class structure:

struct = { Class1, data1:0L, data2:FLTARR(10) } 

any objects created from the class structure Class1 would have the same two fields (data1, a long integer, and data2, a ten-element floating-point array) and any methods associated with the class would have the name Class1::method, where method is the actual name of the method routine. Methods are discussed in detail in Method Routines.

Note
When a new instance of a structure is created from an existing named structure, all of the fields in the newly-created structure are zeroed. This means that fields containing numeric values will contain zeros, fields containing string values will contain null strings, and fields containing pointers or objects will contain null pointers or null objects. In other words, no matter what data the original structure contained, the new structure will contain only a template for that type of data. This is true of objects as well; a newly created object will contain a zeroed copy of the class structure as its instance data.

It is important to realize that creating a class structure does not create an object. Objects can only be created by calling the OBJ_NEW or OBJARR function with the name of the class structure as the argument, and can only be accessed via the returned object reference. In addition, object methods can only be called on objects, and not on class structures themselves.

Once defined, a given class structure type cannot be changed. If a structure definition is executed and the structure already exists, each tag name and the structure of each tag field must agree with the original definition. To redefine a structure, you must either reset or exit the current IDL session.

Automatic Class Structure Definition

If IDL finds a reference to a structure that has not been defined, it will search for a structure definition procedure to define it. (This is true of all structure references, not just class structures.) Automatic structure definition is discussed in Automatic Structure Definition. Briefly, if IDL encounters a structure reference for a structure type that has not been defined, it searches for a routine with a name of the form

STRUCT__DEFINE 

where STRUCT is the name of the structure type. Note that there are two underscores in the name of the structure definition routine.

The following is an example of a structure definition procedure that defines a structure that will be used for the class CNAME.

PRO CNAME__DEFINE 
    struct = { CNAME, data1:0L, data2:FLTARR(10) } 
END 

This defines a structure named CNAME with 2 data fields (data1, a long integer, and data2, a ten-element floating-point array). If you tell IDL to create an object of type CNAME before this structure has been defined, IDL will search for the procedure CNAME__DEFINE to define the class structure before attempting to create the object. If the CNAME__DEFINE procedure has not yet been compiled, IDL will use its normal routine searching algorithm to attempt to find a file named CNAME__DEFINE.PRO. If IDL cannot find a defined structure or structure definition routine, the object-creation operation will fail.

Note
If you are creating structure definitions on the fly, the possibility exists that you will run into namespace conflicts - that is, a structure with the same name as the structure you are attempting to create may already exist. This can be a problem if you are developing object-oriented applications for others, since you probably do not have much control over the IDL environment on your clients' systems. You can avoid most problems by creating a unique namespace for your routines; RSI does this by prefixing the names of objects with the letters "IDL". To be completely sure that the objects created by your programs are what you expect, however, you should have the program inspect the created structures and handle errors appropriately.


Home | Categories | Alphabetical | Classes | All Contents | [ < ] | [ > ]