Petr Schreiber
12-02-2014, 00:17
I just discovered the member variable duplication is checked when using inheritance, but in case of functions it is not the case.
This allows cool tricks like:
Uses "Console"
Type Vector2D
x As Single
y As Single
ToString As Function
End Type
Function Vector2D.ToString() As String
Return Format$(Me.x, "#.00")+","+Format$(Me.y, "#.00")
End Function
' --
Type Vector3D
Vector2D
z As Single
ToString As Function ' -- Overrides the base ToString automagically
End Type
Function Vector3D.ToString() As String
Return Format$(Me.x, "#.00")+","+Format$(Me.y, "#.00")+","+Format$(Me.z, "#.00")
End Function
Dim v2 As Vector2D
v2.x = 1
v2.y = 2
Dim v3 As Vector3D
v3.x = 1
v3.y = 2
v3.z = 3
PrintL "v2 = " + v2.ToString()
PrintL "v3 = " + v3.ToString()
WaitKey
Here I simply redefine the ToString in ancestor and no error is raised.
Question for further discussion - is it good or bad :)?
Maybe such a hacks should be allowed, but only with OVERRIDE prefix for the members (both variables and functions)?
Petr
This allows cool tricks like:
Uses "Console"
Type Vector2D
x As Single
y As Single
ToString As Function
End Type
Function Vector2D.ToString() As String
Return Format$(Me.x, "#.00")+","+Format$(Me.y, "#.00")
End Function
' --
Type Vector3D
Vector2D
z As Single
ToString As Function ' -- Overrides the base ToString automagically
End Type
Function Vector3D.ToString() As String
Return Format$(Me.x, "#.00")+","+Format$(Me.y, "#.00")+","+Format$(Me.z, "#.00")
End Function
Dim v2 As Vector2D
v2.x = 1
v2.y = 2
Dim v3 As Vector3D
v3.x = 1
v3.y = 2
v3.z = 3
PrintL "v2 = " + v2.ToString()
PrintL "v3 = " + v3.ToString()
WaitKey
Here I simply redefine the ToString in ancestor and no error is raised.
Question for further discussion - is it good or bad :)?
Maybe such a hacks should be allowed, but only with OVERRIDE prefix for the members (both variables and functions)?
Petr