IDL objects can have associated procedures and functions called methods. Methods are called on objects via their object references using the method invocation operator.
While object methods are constructed in the same was as any other IDL procedure or function, they are different from other routines in the following ways:
Method routines are defined in the same way as other IDL procedures and functions, with the exception that the name of the class to which they belong, along with two colons, is prepended to the method name:
PRO ClassName::Method IDL statements END
or
FUNCTION ClassName::Method, Argument1 IDL statements RETURN, value END
For example, suppose we create two objects, each with its own "print" method.
First, define two class structures:
struct = { class1, data1:0.0 }
struct = { class2, data2a:0, data2b:0L, INHERITS class1 }
Now we define two "print" methods to print the contents of any objects of either of these two classes. (If you are typing this at the IDL command line, enter the .RUN command before each of the following procedure definitions.)
PRO class1::Print1 PRINT, self.data1 END PRO class2::Print2 PRINT, self.data1 PRINT, self.data2a, self.data2b END
Once these procedures are defined, any objects of class1 have access to the method Print1, and any objects of class2 have access to both Print1 and Print2 (because class2 is a subclass of-it inherits from-class1). Note that the Print2 method prints the data1 field inherited from class1.
| Note |
Every method routine has an implicit argument parameter named self. The self parameter always contains the object reference of the object on which the method is called. In the method routines created above, self is used to specify which object the data fields should be printed from.
You do not need to explicitly pass the self argument; in fact, if you try to specify an argument called self when defining a method routine, IDL will issue an error.
You must use the method invocation operator (->) to call a method on an object. The syntax is slightly different from other routine invocations:
;For a procedure method. ObjRef->Method ;For a function method. Result = ObjRef->Method()
Where ObjRef is an object reference belonging to the same class as the Method, or to one of that class' subclasses. We can illustrate this behavior using the Print1 and Print2 methods defined above.
First, define two new objects:
A = OBJ_NEW('class1')
B = OBJ_NEW('class2')
We can call Print1 on the object A as follows:
A->Print1
IDL prints:
0.00000
Similarly, we can call Print2 on the object B:
B->Print2
IDL prints:
0.00000 0 0
Since the object B inherits its properties from class1, we can also call Print1 on the object B:
B->Print1
IDL prints:
0.00000
We cannot, however, call Print2 on the object A, since class1 does not inherit the properties of class2:
A->Print2
IDL prints:
% Attempt to call undefined method: 'CLASS1::PRINT2'.
When a method is called on an object reference, IDL searches for it as with any procedure or function, and calls it if it can be found, following the naming convention established for structure definition routines. (See Automatic Class Structure Definition.) In other words, IDL discovers methods as it needs them in the same way as regular procedures and functions, with the exception that it searches for files named
classname__method.pro
rather than simply
method.pro
Remember that there are two underscores in the file name, and two colons in the method routine's name.
| Note |