Overloading operators
From REALbasicWiki
| Overall article skill | ✭ |
There are some cases where you would like to make:
- An automatic conversion of one class into a number or another class
- Use an operator (+, -, /, *, etc) between your classes without having to create a special method to do that
As an example, we will make a new class which handles complex numbers. Just to make short, a complex number is a number composed of a real part and an imaginary part, both being a double value in the REALbasic world. A real number is a complex number with a null imaginary part. So a real number is always also a complex number, whereas a complex number can be a real number only if its imaginary part is null.
Contents |
[edit] The "Complex" class
Our new class, simply named Complex, will then be a simple class with no superclass and having two properties:
Now we can create a complex number with:
dim C as Complex
C = new Complex
C.Re = 1.0
C.Im = 2.0
[edit] Constructor(s)
Of course, a simple constructor will make things easier:
Sub Constructor( realvalue as double, imaginaryvalue as double = 0.0 )
me.Re = realvalue
me.Im = imaginaryvalue
End Sub
//We need to explicitly create the default constructor or we can no longer use C = new Complex() without arguments
Sub Constructor()
End Sub
And now we can write
dim C as Complex
C = new Complex( 2.78654, -62.98 )
//Imaginary value is optional, in which case it is null
C = new Complex( 2.78654 )
[edit] Operator overloading - Level 1
Now remember that a real number is also a complex number, so it would be nice if we could just write C = 2.78654 instead of C = new Complex( 2.78654 ). This is where operator overloading comes.
More especially, we will use the method Operator_Convert which is in charge of converting the class to or from another type.
Sub Operator_Convert( realvalue as double )
me.Re = realvalue
me.Im = 0.0
End sub
And now the magic !
dim C1, C2 as Complex
C1 = new Complex
C1 = 2.0
//and even better
C2 = 2.0 //We do not even need the "new"
And because REALbasic has some built-in Operator_Convert for its different datatypes, you can also create a new complex from integer(s). REALbasic detects that you need a double value so it automatically converts the integer into a double and then it passes the double value to your function.
[edit] Overloading operators - Level 2
Now that we can easily create a Complex instance, we should begin doing something with it. The easiest is probably to add two complex numbers. We can achieve that by making a new global method called AddComplex:
//In a module
Function AddComplex( C1 as Complex, C2 as Complex ) as Complex
dim Result as new Complex
Result.Re = C1.Re + C2.Re
Result.Im = C1.Im + C2.Im
return Result
End Function
But we can do better, again with operator overloading. One of such operators is Operator_Add which is called when you use "+" as in "myResult = 2 + 8". By default, REALbasic does not know how to add two classes together, but you can tell it how to do this:
Function Operator_Add( C2 as Complex ) as Complex
//"self"" represents the first operand and C2 is the complex after the + sign
dim Result as new Complex
Result.Re = self.Re + C2.Re
Result.Im = self.Im + C2.Im
return Result
End Function
Now when REALbasic encounters a Complex + Complex operation, it will call the Operator_Add function of the first complex object, so you can write:
Dim C, C1, C2 as Complex
C1 = new Complex(1.0, 2.0)
C2 = new Complex(3.5, -8.0)
C = C1 + C2
//C will then be a Complex (4.5, -6.0)
And remember that we implemented a Operator_Convert to automatically convert a double into a Complex, so you can also add a double to a Complex (the double will be converted into a Complex before being passed to the Operator_Add method). Then you can write:
Dim C, C1 as Complex
C1 = new Complex(1.0, 2.0)
C = C1 + 8.0
//C will be a Complex (9.0, 2.0)
Now let's try the other way:
Dim C, C1 as Complex
C1 = new Complex( 2.0, 8.0)
C = 1.0 + C1
//---->>> Compiler complains that the operator is not compatible with the data types specified
But why ? Well, there are some cases where "a + b" is not equivalent to "b + a", that is why a programming language should make the difference between those two operations. This is also why REALbasic has another operator under the hood: Operator_AddRight.
You have noticed that self represents the first operand in Operator_Add and the second operand is passed as a parameter. With Operator_AddRight, self is the second operand and the parameter is the first one.
Function Operator_AddRight( theValue as double) as Complex
Dim Result as Complex
Result = new Complex
Result.Re = self.Re + theValue
Result.Im = self.Im
return Result
End Function
And now it works !
[edit] More operators
Now you have understood the Operator_Add and Operator_AddRight operators and what you can do if you overload them, here is the list of all the other operators that should ideally be implemented as well (note that not all of them are applicable to our Complex class):
- Operator_And(Right)
- Operator_Divide(Right)
- Operator_IntegerDivide(Right)
- Operator_Modulo(Right)
- Operator_Multiply(Right)
- Operator_Not
- Operator_Or(Right)
- Operator_Power(Right)
- Operator_Subtract(Right)
- Operator_Negate
- Operator_Compare
[edit] See also
- Complex Utilities (by Roger Meier) is a reusable class/module implementing almost everything you need to handle complex numbers.
