Language > Objects > Operator Overloading > Creating Operator Overloading Methods

# Creating Operator Overloading Methods

To overload an object operator:

1. Enable operator overloading for the class by inheriting the IDL_Object class. Refer to IDL_Object for more information.
2. Add an `_overload*` method to the object that redefines the operator behavior. The valid overloaded operator method names are listed in Overloaded Object Operator Method Names.

## Binary Operators

For binary operators, the user-supplied method must be a function that accepts two input arguments. The value on the left side of the operator is passed as the first argument, and the value on the right side of the operator is passed as the second argument. The leftmost object that defines an `_overload` method for the operator is passed as the implicit self argument. The function can return any value.

For example, the following shows a simple overloading of the bitwise AND operator that returns 1 if both operands are objects or zero if only one operand is an object.

FUNCTION className::_overloadAND, arg1, arg2

RETURN, (SIZE(arg1, /TYPE) EQ 11) && $

(SIZE(arg2, /TYPE) EQ 11) ? 1 : 0

END

## Unary Operators

For most unary operators, the user-supplied method must be a function that accepts no arguments. The object being operated on is used as the implicit self argument. For example, the following shows a simple overloading of the `-` (negation) operator that returns a string denying that the object is of the class that it actually is:

FUNCTION className::_overloadMinusUnary

RETURN, 'I am not a '+OBJ_CLASS(self)

END

## Information Routines

For the overloadable information routines (HELP, PRINT, and SIZE), the user-supplied method must be a function that returns values appropriate for the specific information routine:

#### HELP

The `_overloadHelp` method accepts a single string argument (the name of the variable passed to the HELP procedure), and must return a string or array of strings, which will be printed to the console as the output from the HELP procedure. For example:

FUNCTION className::_overloadHelp, Varname

class = OBJ_CLASS(self)

text = 'The '+Varname+' variable is an object of class '+class

RETURN, text

END

#### PRINT

The `_overloadPrint` method can return a variable of any datatype, which will be passed to the PRINT procedure and printed with the default formatting. For example, the following method definition:

FUNCTION className::_overloadPrint

self->GetProperty, OBJ_DATA = data

RETURN, data

END

would print out the value of the object’s OBJ_DATA property.

#### SIZE

The `_overloadSize` method must return a scalar or vector that specifies the number of elements in each “dimension” of the object, as if it were an array of one or more dimensions. For example, the following method definition returns the dimensions of the variable contained in the object’s OBJ_DATA field:

FUNCTION className::_overloadSize

self->GetProperty, OBJ_DATA = data

RETURN, SIZE(data, /DIMENSIONS)

END

The value returned by the `_overloadSize` method is used by the SIZE procedure to construct the number of elements, the number of dimensions, and the size of each dimension of the object. Other values reported by SIZE (such as the data type code or type name) are not altered by the `_overloadSize` method.

Note that the N_ELEMENTS function will return the product of the values returned by the `_overloadSize` method as the total number of elements of the object.