연산자 중복정의(operator overload)

VB2005는 VB2003과 달리 연산자 중복정의를 할 수 있다. 다음은 행렬계산에서 곱셈(*)에 대한 연산자 중복정의를 한 코드이다.

Public Shared Operator *(ByVal mA As Matrix3D, ByVal mB As Matrix3D) As Matrix3D
    Dim i, j As Short
    Dim mC As Matrix3D = New Matrix3D
    For i = 0 To 3
        For j = 0 To 3
          mC.Data(i, j) = (mA.Data(i,0)*mB.Data(0,j))+(mA.Data(i,1)*mB.Data(1,j)) _
                        + (mA.Data(i,2)*mB.Data(2,j))+(mA.Data(i,3)*mB.Data(3,j))
        Next j
    Next i
    Return mC
End Operator

Public Shared Operator *(ByVal mA As Matrix3D, ByVal p1 As Point3D) As Point3D
  Dim p2 As Point3D = New Point3D()
  p2.X=(mA.Data(0,0)*p1.X)+(mA.Data(0,1)*p1.Y)+(mA.Data(0,2)*p1.Z)+(mA.Data(0,3)*1)
  p2.Y=(mA.Data(1,0)*p1.X)+(mA.Data(1,1)*p1.Y)+(mA.Data(1,2)*p1.Z)+(mA.Data(1,3)*1)
  p2.Z=(mA.Data(2,0)*p1.X)+(mA.Data(2,1)*p1.Y)+(mA.Data(2,2)*p1.Z)+(mA.Data(2,3)*1)
  Return p2
End Operator
Print Friendly, PDF & Email