Using Operations with Objects

Object reference variables are not directly usable by many of the operators, functions, or procedures provided by IDL. You cannot, for example, do arithmetic on them or plot them. You can, of course, do these things with the contents of the structures contained in the object heap variables referred to by object references, assuming that they contain non-object data.

There are four IDL operators that work with object reference variables: assignment, method invocation (described in Acting on Objects Using Methods), EQ, and NE. The remaining operators (addition, subtraction, etc.) do not make any sense for object references and are not defined.

Note
The structure dot operator (.) is allowed within methods of a class of a custom object. See The Implicit Self Argument for details.

Many non-computational functions and procedures in IDL do work with object references. Examples are SIZE, N_ELEMENTS, HELP, and PRINT. It is worth noting that the only I/O allowed directly on object reference variables is default formatted output, in which they are printed as a symbolic description of the heap variable they refer to. This is merely a debugging aid for the IDL programmer—input/output of object reference variables does not make sense in general and is not allowed. Please note that this does not imply that I/O on the contents of non-object instance data contained in heap variables is not allowed. Passing non-object instance data contained in an object heap variable to the PRINT command is a simple example of this type of I/O.

You can also get information about an object as described in Returning Object Type and Validity.

Object Assignment

Assignment works in the expected manner—assigning an object reference to a variable gives you another variable with the same reference. Hence, after executing the statements:

;Define a class structure. 
struct = { cname, data1:0.0 } 
 
;Create an object. 
A = OBJ_NEW('cname') 
 
;Create a second object reference. 
B = A 
 
HELP, A, B 

IDL prints:

A               OBJREF    = <ObjHeapVar1(CNAME)> 
B               OBJREF    = <ObjHeapVar1(CNAME)> 

Note that both A and B are references to the same object heap variable.

Object Equality and Inequality

The EQ and NE operators allow you to compare object references to see if they refer to the same object heap variable. For example:

;Define a class structure. 
struct = {cname, data:0.0} 
 
;Create an object. 
A = OBJ_NEW('CNAME') 
 
;B refers to the same object as A. 
B = A 
 
;C contains a null object reference. 
C = OBJ_NEW() 
 
PRINT, 'A EQ B: ', A EQ B & $ 
PRINT, 'A NE B: ', A NE B & $ 
PRINT, 'A EQ C: ', A EQ C & $ 
PRINT, 'C EQ NULL: ', C EQ OBJ_NEW() & $ 
PRINT, 'C NE NULL:', C NE OBJ_NEW() 

IDL prints:

A EQ B:    1 
A NE B:    0 
A EQ C:    0 
C EQ NULL: 1 
C NE NULL: 0