xHarbour Reference Documentation > Statement Reference xHarbour Developers Network  

OPERATOR

Overloads an operator and declares a method to be invoked for it.

Syntax

OPERATOR <cOperator> [ARG <operand>] INLINE <expression>

Arguments

<cOperator>
This is the operator which executes <expression>. The operator must be enclosed in quotes.
ARG <operand>
When a binary operator is overloaded, <operand> is the name of a variable which receives the value of the second operand of the operator. Unary operators do not require <operand> be declared.
INLINE <expression>
This is the expression which is executed when the object is an operand of <operator>. <expression> must be one line of code. The code cannot contain commands but only function and method calls.

Description

The OPERATOR statement can only be used in the class declaration between CLASS and ENDCLASS. It is the inline version of the METHOD...OPERATOR statement.

Note:  only regular operators can be used for <cOperator>. Special operators are not supported. when <cOperator> is a binary operator, the object must be the left operand for <expression> being executed.

Info

See also:METHOD...OPERATOR
Category: Class declaration , Declaration , xHarbour extensions
Header:hbclass.ch
Source:vm\classes.c
LIB:xhb.lib
DLL:xhbdll.dll

Example

// The example declares a String class and displays the
// results of operations with a String object.

   #include "HbClass.ch"

   PROCEDURE Main
      LOCAL obj := String():New( "Hello  " )

      ? obj =  "Hello"            // result: .T.
      ? obj == "Hello"            // result: .F.
      ? obj != "Hello"            // result: .F.
      ? obj <> "Hello"            // result: .F.
      ? obj #  "Hello"            // result: .F.
      ? obj $  "Hello"            // result: .F.

      ? obj <  "hello"            // result: .T.
      ? obj <= "hello"            // result: .T.
      ? obj >  "hello"            // result: .F.
      ? obj >= "hello"            // result: .F.

      ? obj +  "World"            // result: Hello  World
      ? obj -  "World"            // result: HelloWorld
   RETURN


   CLASS String
      EXPORTED:
      DATA value       INIT ""

      METHOD init( c ) INLINE ( IIf(valtype(c)=="C", ::value := c, ), self )

      OPERATOR "="   ARG c INLINE ::value =  c
      OPERATOR "=="  ARG c INLINE ::value == c
      OPERATOR "!="  ARG c INLINE ::value != c
      OPERATOR "<"   ARG c INLINE ::value <  c
      OPERATOR "<="  ARG c INLINE ::value <= c
      OPERATOR ">"   ARG c INLINE ::value >  c
      OPERATOR ">="  ARG c INLINE ::value >= c
      OPERATOR "+"   ARG c INLINE ::value +  c
      OPERATOR "-"   ARG c INLINE ::value -  c
      OPERATOR "$"   ARG c INLINE ::value $  c
   ENDCLASS

Copyright © 2006-2007 xHarbour.com Inc. All rights reserved.
http://www.xHarbour.com
Created by docmaker.exe