Welcome to the Harris Geospatial product documentation center. Here you will find reference guides, help documents, and product libraries.


Harris Geospatial / Docs Center / Using IDL / Object Operator Overloading Overview

IDL

Object Operator Overloading Overview

Operator Overloading Summary

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 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, oLengthA, oLengthB

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 Operators for details.

Overloading Informational Routines


When the Implied Print, 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 Procedures.



© 2017 Exelis Visual Information Solutions, Inc. |  Legal
My Account    |    Buy    |    Contact Us