Combining routines and code in objects can be a powerful programming technique, but sometimes it is convenient to treat objects like basic types. For example, to add the properties of two objects, object-oriented programming requires you to call the property access methods, add the returned values, and store the value (possibly in the property of a third object that you must create). Operator overloading, introduced in IDL 8.0, allows you to move this potentially complex code into an object method and invoke it using standard operator syntax.
Note: To use object operator overloading, at least one of the operands must be an object. In the case of unary operators, the operand must be a scalar object that defines the overloaded operator method. For binary operators, at least one of the operands must be a scalar object that defines the overloaded operator method, and the second operand can be of any type.
For example, suppose you have an object class named lengthObject, each instance of which has a LENGTH property. Given two lengthObjects, you could do the following to create a third object whose LENGTH property was equal to the sum of the first two:
oLengthC = OBJ_NEW('lengthObject')
oLengthA->GETPROPERTY, LENGTH = lengthA
oLengthB->GETPROPERTY, LENGTH = lengthB
oLengthC->SETPROPERTY, LENGTH = lengthA + lengthB
If you perform this operation often, it might make sense to create an object method that handles the details of getting and setting properties. You might, for example, create an AddLengths method to the lengthObject class as follows:
PRO lengthObject::AddLengths, oLengthA, oLengthB
oLengthA->GETPROPERTY, LENGTH = lengthA
oLengthB->GETPROPERTY, LENGTH = lengthB
self->SETPROPERTY, LENGTH = lengthA + lengthB
END
With this method in place, the code to add the lengths looks like:
oLengthC = OBJ_NEW('lengthObject')
oLengthC->AddLengths, lengthA, lengthB
Object operator overloading offers an even simpler alternative. By writing an operator overload method for the lengthObject’s plus (+) operator rather than the AddLengths method, we can add length objects as if they were simple variables:
oLengthC = oLengthA + oLengthB
The custom-written overloaded plus method would handle the details of reading and writing to the given objects, creating a new lengthObject, and storing the sum in the new object’s LENGTH property. In this example, the operator overload method might look something like this:
FUNCTION lengthObject::_overloadPlus, oLengthA, oLengthB
oLengthC = OBJ_NEW('lengthObject')
oLengthA->GETPROPERTY, LENGTH = lengthA
oLengthB->GETPROPERTY, LENGTH = lengthB
oLengthC->SETPROPERTY, LENGTH = lengthA + lengthB
RETURN, oLengthC
END
By overloading the + operator for this object class, we can move the complexity of the code that adds the LENGTH values into the object class itself, and dramatically simplify the higher-level code.
Note: Not all of IDL’s standard operators can be overloaded. See Overloadable Operatorsfor details.
While overloading standard operators for an object class allows you to write higher-level code that appears clean and simple, you should remain aware of the potential for confusion when your object class changes the standard meaning of an operator.
Although it is intuitive to create an overloaded method that reflects the operator’s behavior, this is not mandatory. In fact, you are free to implement the operator overload method to do anything you wish. In the previous example, we could have implemented the lengthObject’s plus method to sum vectors, returning a third vector. We could implement a plus method that performs some operation completely unrelated to addition. The functionality of an overloaded method is completely up to you, and is not visible to someone reading your high-level code. Unless the person reading your code has access to the object method definition files, there may be no way to determine exactly what a given operator is doing.
When the PRINT/PRINTF, HELP, SIZE, and N_ELEMENTS routines are called with overloaded objects, the routines query the object’s appropriate _overload method for the information to return. You can write your own _overload methods to change what information these routines return. For discussions on overloading these routines, see Overloading the SIZE and N_ELEMENTS Functions and Overloading the HELP and PRINT/PRINTF Procedures.