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.

; Initialize a colorCircle_doc object. Use an IDLgrPolygon to 
; create the actual circle, using the color value specified at
; creation. If present, the POSITION keyword is used to position
; multiple circles so that they do not overlap.
 
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
 
; Overload the "+" (plus) operator for the colorCircle_doc object
; Adding two colorCircle_doc objects sums the colors
 
FUNCTION colorCircle_doc::_overloadplus, input1, input2
 
   ; Return to the calling routine if an error occurs
   ON_ERROR, 2
 
   msg = 'Specify ColorCircle objects or three-element vectors'
 
   ; Only perform the addition if the input arguments are of the
   ; correct form -- either a colorCircle_doc object or a
   ; three-element [r,g,b] vector.
   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
 
   ; Here we assume an additive light model, where each color
   ; channel is the sum of the inputs' color channels.
   ; The sum can not be greater than 255.
   c3 = (FIX(c1) + c2) < 255
 
   ; Create a new colorCircle_doc object with the new color,
   ; and return a reference to the object.
   oNew=OBJ_NEW('colorCircle_doc', COLOR=c3, POSITION=2)
   RETURN, oNew
 
END
 
; Define the colorCircle_doc object
; Note that this class inherits from both the IDLgrPolygon
; and IDL_Object classes.
 
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.

; Create some colorCircle_doc objects
CircleA = ColorCircle_doc(COLOR=[255,0,0], POSITION=0)
CircleB = ColorCircle_doc(COLOR=[0,0,255], POSITION=1)
; Add the objects
CircleC = CircleA + CircleB
PRINT, CircleC.color
; Create a model to hold the graphic objects
oCircleModel = OBJ_NEW('IDLgrModel', LIGHTING=0)
; Add the objects to the model
oCircleModel.Add, CircleA
oCircleModel.Add, CircleB
oCircleModel.Add, CircleC
; Draw the view with XOBJVIEW
XOBJVIEW, oCircleModel, /BLOCK, $
   TITLE='Object Overloading--Color Addition'