• No results found

METHOD...OPERATOR

In document xHarbour Language Reference Guide (Page 160-165)

METHOD...OPERATOR

Declares a method to be executed with an operator.

Syntax

METHOD <MethodName> OPERATOR <xOperator>

Arguments

<MethodName>

This is the symbolic name of the method to declare. It must begin with a letter or underscore followed by digits, letters or underscores. The symbolic name can contain up to 63 characters.

OPERATOR <xOperator>

This is the operator which invokes the method.

Description

The OPERATOR option of the METHOD statement defines an operator that can be used with an object. When the object is an operand of <xOperator>, the method <MethodName> is invoked. Unary operators invoke the method without an argument while binary operators pass the second operand as argument to the method.

Note: only regular operators can be used for <xOperator>. Special operators are not supported. When

<xOperator> is a binary operator, the object must be the left operand for invoking the method

<MethodName>.

Info

See also: CLASS,EXTEND CLASS...WITH METHOD,METHOD (implementation), OPERATOR

Category: Class declaration,Declaration,xHarbour extensions Header: hbclass.ch

Source: vm\classes.c

LIB: xhb.lib

DLL: xhbdll.dll

Example

// The example demonstrates how basic numeric operations can // be implemented with objects. The example support two unary // and two binary operators.

#include "Hbclass.ch"

PROCEDURE Main

LOCAL oNumA := Number():new( 10 ) LOCAL oNumB := NUmber():new( 2 )

? oNumA + 100 // result: 110

? oNumA + oNumB ++ // result: 13

? oNumA - oNumB // result: 7

METHOD...OPERATOR

? -- oNumA // result: 9

RETURN

CLASS Number PROTECTED:

DATA nNumber INIT 0 EXPORTED:

METHOD init(n) INLINE (IIf(Valtype(n)=="N",::nNumber:=n,) , self) METHOD add OPERATOR +

METHOD subtract OPERATOR -METHOD increment OPERATOR ++

METHOD decrement OPERATOR --ACCESS value INLINE ::nNumber ASSIGN value(n) INLINE ::nNumber := n ENDCLASS

METHOD add(x) CLASS Number IF HB_IsObject(x)

RETURN ::nNumber + x:value ENDIF

RETURN ::nNumber + x

METHOD subtract(x) CLASS Number IF HB_IsObject(x)

RETURN ::nNumber - x:value ENDIF

RETURN ::nNumber - x

METHOD increment CLASS Number RETURN ++ ::nNumber

METHOD decrement CLASS Number RETURN -- ::nNumber

METHOD...VIRTUAL

METHOD...VIRTUAL

Declares a method as virtual.

Syntax

METHOD <MethodName> VIRTUAL|DEFERRED

Arguments

<MethodName>

This is the symbolic name of the method to declare. It must begin with a letter or underscore followed by digits, letters or underscores. The symbolic name can contain up to 63 characters.

DEFERRED

This is a synonym for VIRTUAL.

Description

The METHOD...VIRTUAL statement can only be used in the class declarationbetween CLASS and ENDCLASS. It declares the symbolic name of a virtual method.

A virtual method is a method that is declared in a class but not implemented. The implementation of such method is deferred to a sub-class. Consequently, virtual methods are only used with inheritance where sub-classes are derived from super classes. By means of virual methods a programmer can define the "message profile" within a class hierarchy at a very early point of the development cycle. An object having virtual methods understands the corresponding message but does not execute code.

Info

See also: CLASS,DELEGATE

Category: Class declaration,Declaration,xHarbour extensions Header: hbclass.ch

Source: vm\classes.c

LIB: xhb.lib

DLL: xhbdll.dll

OPERATOR

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 theclass declarationbetween CLASS and ENDCLASS. It is the inline version of theMETHOD...OPERATORstatement.

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.

OPERATOR

? 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

In document xHarbour Language Reference Guide (Page 160-165)