Code: Select all
#Include Once "windows.bi"
#Include Once "vbcompat.bi"
#Include Once "crt.bi"
Type TMatrix
Private:
Arr(1 To 2, 1 To 2) As Double
Public:
Declare Constructor
Declare Destructor Cdecl
Declare Function at(row As Integer, col As Integer) ByRef As Double
End Type
Constructor TMatrix()
Print "in Constructor"
End Constructor
Destructor TMatrix()
Erase Arr
Print "in Destructor"
End Destructor
Function TMatrix.at(irow As Integer,icol As Integer) ByRef As Double
If irow<1 Or irow>2 _
Or icol<1 Or icol>2 Then
Messagebox NULL , "Les indices sont en dehors de la plage (" & irow & ";" & icol & ")" ,"erreur dans Matrix.at",MB_ICONERROR
Exit Function
EndIf
Return Arr(irow,icol)
End Function
Function DET(ByVal m As TMatrix) As Double
Print "in DET"
Return m.at(1,1)*m.at(2,2) - M.at(1,2)*m.at(2,1) ' the destructor is called but m is still valid
End Function
Function DET2(ByRef m As TMatrix) As Double
Print "in DET"
Return m.at(1,1)*m.at(2,2) - M.at(1,2)*m.at(2,1) ' no destructor is called but m is still valid ok
End Function
Dim M1 As TMatrix
M1.at(1,1)=1.00 : M1.at(1,2)=2.00
M1.at(2,1)=3.00 : M1.at(2,2)=5.00
For i As Integer=1 To 2
For j As Integer=1 To 2
Print M1.at(i,j);
Next j
Print
Next
Print " det(m)= " & DET(m1)
Print " after byval call "
For i As Integer=1 To 2
For j As Integer=1 To 2
Print M1.at(i,j);
Next j
Print
Next
Print
Print " det2(m)= " & DET2(m1)
Print " after byref call "
For i As Integer=1 To 2
For j As Integer=1 To 2
Print M1.at(i,j);
Next j
Print
Next
Print
Sleep