Prev | Current Page 201 | Next

Guy Fouch?©, Trey Nash

"Accelerated VB 2008"

0, 2.0)
Dim cpx2 = cpx1 + 20.0
This saves you the time of having to create an extra Complex instance with just the Real
member variable set to 20.0 in order to add it to cpx1. Here, the first operand is of type
Complex, and the second operand is of type Double. However, suppose you want to be able to
reverse the operands on the operator and do something like the following instead:
Dim cpx2 = 20.0 + cpx1
In this case, the first operand is of type Double, and the second operand is of type Complex.
If you want to support different orderings of operands of different types, you must provide different
overloads of the operator. If you overload a binary operator that uses different
parameter types, you can create a mirror overload??”that is, another operator method that
reverses the parameters.
CHAPTER 7 n OPERATOR OVERLOADING 120
Overloading the Addition Operator
Let??™s add more operators to the Complex structure and build upon it throughout the chapter:
Imports System
Public Structure Complex
Private Real As Double
Private Imaginary As Double
Public Sub New(ByVal real As Double, ByVal imaginary As Double)
Me.Real = real
Me.Imaginary = imaginary
End Sub
Public Shared Function Add _
(ByVal lhs As Complex, ByVal rhs As Complex) As Complex
Return New Complex(lhs.


Pages:
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
katalog stron dieta light Altanki wierszyki gry on line