[<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]
 OPERATOR METHOD                 Define an Operator overload Method
------------------------------------------------------------------------------


 SYNTAX:

  METHOD <cMsg> OPERATOR <uOp>


 PARAMETER:

  <cMsg>  Is the Method name of our Operator-overload Method

  <uOp>   Is the Operator that is overloaded. This can be one of the 
          following Clipper operators :

          []  Indicator access/assing array element: a[x] / a[x]:=3
           +  binary    sum:                         a + b, a += b
          ++  unary     increment:                   a++, ++a
           -  binary    subtract:                    a - b, a -= b
          --  unary     decrement:                   a--, --a
           *  binary    multiply:                    a * b, a *= b
           /  binary    divide:                      a / b, a /= b
          ^  binary    exponentation:               a  b, a = b
           %  binary    modulo:                      a % b, a %= b
           =  binary    equal                        a = b
           == binary    Exactly equal                a == b
           >  binary    Greater than                 a > b 
           >= binary    Greatet than or equal        a >= b
           <  binary    Less than                    a < b
           <= binary    Less than or equal           a <= b



 DESCRIPTION:

  Like Method overloading does for Methods ( replacing a Method of an
  inherited class with a  new one ), Operator overload gives us the power to
  change the behaviour of our Operators, like '==' or '+'. Simply spoken, it
  lets you define a new action for the case that one of the above operators
  is applied on an object of your class. You already use some overloaded 
  Operators in Clipper without knowing it: the '+' operator is not only used
  to sum two numbers, but also to concatenate two strings !

  Lets look at the simple 'exact equal' operator '==' : if you say
  ? obj1 == obj2, this will return a FALSE if obj1 and obj2 are not the same
  Object ( or don't reference the same object ). If you overload the "==" 
  Operator with a new Method, then this Method is called instead of the
  default "==" handler, and in your Method you can Return TRUE or FALSE
  based on your own logic for this class ..  

  For Example:

    +--------------------------------------------------------------+
    |  /* Operator Override example */                             |
    |  METHOD CompAtoB OPERATOR "=="                               |
    |  ...                                                         |
    | METHOD CompAtoB( Orange )                                    |
    |   If ::Apple:name == Orange:name                             |
    |      Return .T.                                              |
    |   ENDIF                                                      |
    | RETURN .F.                                                   |
    +--------------------------------------------------------------+


  You see, with Operator override and the Method CompAtoB, you _can_ compare
  Apples to Oranges <g>. The same applies for the other Operators ..


  More Examples:

    +--------------------------------------------------------------+
    |  /* Use the Array [] Operator to Strings! */                 |
    |  CLASS TestArray                                             |
    |    VAR cData                                                 |
    |                                                              |
    |    METHOD NEW( cData ) ;                                     |
    |     INLINE ( ::cData := cData, Self )                        |
    |                                                              |
    |    METHOD ArrayAt ;                                          |
    |     OPERATOR []                                              |
    |                                                              |
    |  ENDCLASS                                                    |
    |                                                              |
    |  [ ... ]                                                     |
    |                                                              |
    |                                                              |
    |  METHOD ArrAt( nIndex, uNewVal )                             |
    |    IF nIndex > 0 .and. nIndex <= 30                          |
    |       IF pCount() > 1                                        |
    |          ::cData = Stuff( ::cData, ;                         |
    |                           nIndex, 1, ;                       |
    |                           IIF( ValType( uNewVal )=="C",;     | 
    |                                left( uNewVal, 1 ),;          |  
    |                                 Chr( uNewVal ) ) )           |
    |       ELSE                                                   |
    |          uNewVal = SubStr( ::cData, nIndex, 1 )              |
    |       ENDIF                                                  |
    |       RETURN uNewVal                                         |
    |                                                              |    
    |    ENDIF                                                     |
    |  RETURN Self                                                 |
    |                                                              |
    |  FUNCTION TestOp()                                           |
    |   LOCAL ;                                                    |
    |      oString := TestArray():New( "Duckhead" )                |
    |                                                              |
    |  /* Now change one Char in our Duckhead String : */          |
    |   oString[2] := "i"    // this calls the ArrayAt Method      |
    |                                                              |
    |  ? oString:cData                                             |
    |                                                              |
    +--------------------------------------------------------------+




See Also: Method Virtual SetGet Extern Block Inline
This page created by ng2html v1.05, the Norton guide to HTML conversion utility. Written by Dave Pearson