Language > Objects > Operator Overloading > Example: Modifying an Object Data Array using Operator Overloading

# Example: Modifying an Object Data Array using Operator Overloading

This example demonstrates a use of operator overloading with the `[]` (array indexing) operator. We create an object class (store_array_doc) that includes in its member data a 100-element floating-point array, initially populated with random values. The object class’ overloaded array indexing operators allow you to insert or retrieve individual scalar array values, using familiar array syntax.

The store_array_doc object definition and operator overloading code listed in this section is contained in the procedure file `store_array_doc__define.pro`, and is located in the `examples/doc/objects` subdirectory of the IDL distribution. To view the file in an IDL editor window, enter .EDIT store_array_doc__define.pro at the IDL command line.

The following code, which is a portion of `store_array_doc__define.pro`, defines the overloaded `[]` (array indexing) operator methods for the store_array_doc object class.

The `_overloadBracketsRightSide` function interprets the array index as an index into the member data array and returns the value of the specified array element:

FUNCTION store_array_doc::_overloadBracketsRightSide, isRange, $

sub1, sub2, sub3, sub4, sub5, sub6, sub7, sub8

IF N_ELEMENTS(isRange) GT 2 THEN BEGIN

RETURN, 'Only two indices are supported'

ENDIF

IF (MAX(isRange) GT 0) THEN BEGIN

RETURN, 'Subscript Ranges are not allowed'

ENDIF

IF N_ELEMENTS(sub1) THEN retVal = self.ARRAY[sub1]

IF N_ELEMENTS(sub2) THEN retVal = [ret, self.ARRAY[sub2]]

RETURN, retVal

END

The `_overloadBracketsLeftSide` procedure interprets the array index as an index into the member data array and changes the value of the specified array element. If the single index zero is provided, the original object is replaced by the specified value.

PRO store_array_doc::_overloadBracketsLeftSide, objRef, rValue, $

isRange, sub1, sub2, sub3, sub4, sub5, sub6, sub7, sub8

IF N_ELEMENTS(isRange) GT 2 THEN BEGIN

PRINT, 'Only two indices supported by this example operator'

RETURN

ENDIF

IF (MAX(isRange) GT 0) THEN BEGIN

PRINT, 'Subscript Ranges are not allowed'

RETURN

ENDIF

IF N_ELEMENTS(isRange) EQ 1 && (sub1 EQ 0) THEN BEGIN

PRINT, 'Replacing original object with specified value'

OBJ_DESTROY, objRef

objRef = rValue

ENDIF ELSE BEGIN

PRINT, 'Replacing elements in the object data array'

IF (N_ELEMENTS(sub1) && N_ELEMENTS(rValue) GT 0) $

THEN self.ARRAY[sub1] = rValue[0]

IF (N_ELEMENTS(sub2) && N_ELEMENTS(rValue) GT 1) $

THEN self.ARRAY[sub2] = rValue[1]

ENDELSE

END

The following code snippet creates a store_array_doc object and displays the value of the 10th element in the member data array:

myObj = OBJ_NEW('store_array_doc')
PRINT, myObj[9]

The following code snippet replaces the value of the 10th element and prints out the new value:

myObj[9] = 3.14159
PRINT, myObj[9]

The following code snippet replaces the entire object with a string:

myObj[0] = 'Now the object is gone'
HELP, myObj

IDL Prints:

MYOBJ STRING = 'Now the object is gone'