realbasic-nug
[Top] [All Lists]

Re: Overloaded Operators

To: REALbasic NUG <realbasic-nug at lists dot realsoftware dot com>
Subject: Re: Overloaded Operators
From: Mars Saxman <mars at realsoftware dot com>
Date: Tue, 30 Nov 2004 11:54:24 -0800
Delivered-to: realbasic-nug at lists dot realsoftware dot com
phil at mobleybros dot com wrote:
 
> Is there a way for me to simplify the code in each of these overloaded
> operators so that I can have something like this:
> 
> Function Operator_Add(x As Integer) As Rational
> 
> Return Me + Operator_Convert(x)
> 
> End Function

Operator methods are just ordinary methods with a specific name and
signature, so you could certainly call the conversion method directly if you
wanted to:

Function Operator_Add(x As Integer) As Rational
    Dim term As New Rational
    term.Operator_Convert( x )
    Return Me + term
End Function

I think it would be simpler and clearer to let the compiler handle the call,
though; I'd recommend using code like this:

Function Operator_Add(x As Integer) As Rational
    Dim term As Rational = x
    Return Me + term
End Function

> Also, I am a little confused by the Operator_Negate().  Is it always
> called with subtraction and if yes, should I use addition in the
> Operator_Subtract method?  An example would help clarify this for me.

This is subtraction:

x = y - z

This is negation:

x = -y

Operator_Subtract handles subtraction and accepts one parameter, for the
value of the right-hand term (or the left-hand term, in the case of
Operator_SubtractRight); Operator_Negate handles only negation and does not
accept any parameters, since the object itself is the only value involved.

Mars Saxman
REAL Software
-- 
REAL World 2005 - The REALbasic User Conference
March 23rd - 25th, 2005, Austin, Texas
<http://www.realsoftware.com/realworld>

_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>

Search the archives of this list here:
<http://support.realsoftware.com/listarchives/lists.html>

<Prev in Thread] Current Thread [Next in Thread>