The colorCircle_doc object definition and operator overloading code listed in this section is contained in the procedure file colorcircle_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 colorcircle_doc__define.pro at the IDL command line.
The previous example was a straightforward implementation of the plus (+) operator. However, you can implement an overloaded operator routine to do less obvious tasks, or even tasks completely unrelated to the implied operation. In this example, we will implement the plus (+) operator to change the color of a circle object.
The following code, located in colorcircle_doc__define.pro, defines the colorCircle_doc object and the overloaded plus operator method.
FUNCTION colorCircle_doc::Init, COLOR=Color, POSITION=position
position = (N_ELEMENTS(position) EQ 0) ? 0 : position
x = SIN(2*!PI/100 * FINDGEN(100))+position*3
y = COS(2*!PI/100 * FINDGEN(100))
v = self->IDLgrPolygon::Init(x, y, COLOR=Color)
RETURN, v
END
FUNCTION colorCircle_doc::_overloadplus, input1, input2
ON_ERROR, 2
msg = 'Specify ColorCircle objects or three-element vectors'
IF (SIZE(input1, /TYPE) EQ 11) && OBJ_ISA(input1, $
'colorCircle_doc') THEN BEGIN
input1->GETPROPERTY, COLOR=c1
ENDIF ELSE BEGIN
IF (N_ELEMENTS(input1) EQ 3) THEN BEGIN
c1 = input1
ENDIF ELSE BEGIN
MESSAGE, msg, LEVEL=0
ENDELSE
ENDELSE
IF (SIZE(input2, /TYPE) EQ 11) && OBJ_ISA(input2, $
'colorCircle_doc') THEN BEGIN
input2->GETPROPERTY, COLOR=c2
ENDIF ELSE BEGIN
IF (N_ELEMENTS(input2) EQ 3) THEN BEGIN
c2 = input2
ENDIF ELSE BEGIN
MESSAGE, msg, LEVEL=0
ENDELSE
ENDELSE
c3 = (FIX(c1) + c2) < 255
oNew=OBJ_NEW('colorCircle_doc', COLOR=c3, POSITION=2)
RETURN, oNew
END
PRO colorCircle_doc__define
STRUCT = { colorCircle_doc, $
INHERITS IDLgrPolygon, $
INHERITS IDL_Object }
END
The following code snippet adds two colorCircle_doc objects and displays the results with XOBJVIEW.
CircleA = ColorCircle_doc(COLOR=[255,0,0], POSITION=0)
CircleB = ColorCircle_doc(COLOR=[0,0,255], POSITION=1)
CircleC = CircleA + CircleB
PRINT, CircleC.color
oCircleModel = OBJ_NEW('IDLgrModel', LIGHTING=0)
oCircleModel.Add, CircleA
oCircleModel.Add, CircleB
oCircleModel.Add, CircleC
XOBJVIEW, oCircleModel, /BLOCK, $
TITLE='Object Overloading--Color Addition'