It is only since fbc 1.10.0 that the 'Procptr (member procedure address)' operator allows virtual member operator chaining when inheritance or polymorphism.
Previously, such chaining was impossible to code safely.
- Example with the 'Cast' member operator:
Code: Select all
Type Parent Extends Object
Dim As String Sp = " Parent"
Declare Virtual Operator Cast() As String
End Type
Operator Parent.Cast() As String
Return This.Sp
End Operator
Type Child Extends Parent
Dim As String Sc = " Child"
Declare Virtual Operator Cast() As String
End Type
Operator Child.Cast() As String
' Print Cast(Parent, This) '' NOK : infinite loop
Print Procptr(Parent.Cast)(This) '' OK
Return This.Sc
End Operator
Dim As Parent p
Dim As Child c '' for inheritance tests
Dim Byref As Parent rpc = c '' for polymorphism tests
Print "'Print p' :"
Print p
Print
Print "'Print c' :"
Print c
Print
Print "'Print rpc' :"
Print rpc
Print
Sleep
Code: Select all
'Print p' : Parent 'Print c' : Parent Child 'Print rpc' : Parent Child
- Example with the '@' member operator:
Code: Select all
Type Parent Extends Object
Dim As String Sp = " Parent"
Declare Virtual Operator @() As String Ptr
End Type
Operator Parent.@() As String Ptr
Return @This.Sp
End Operator
Type Child Extends Parent
Dim As String Sc = " Child"
Declare Virtual Operator @() As String Ptr
End Type
Operator Child.@() As String Ptr
' Print *@Cast(Parent, This) '' NOK : infinite loop
Print *Procptr(Parent.@)(This) '' OK
Return @This.Sc
End Operator
Dim As Parent p
Dim As Child c '' for inheritance tests
Dim Byref As Parent rpc = c '' for polymorphism tests
Print "'Print *@p' :"
Print *@p
Print
Print "'Print *@c' :"
Print *@c
Print
Print "'Print *@rpc' :"
Print *@rpc
Print
Sleep
Code: Select all
'Print *@p' : Parent 'Print *@c' : Parent Child 'Print *@rpc' : Parent Child
- Example with the 'Let' member operator:
Code: Select all
Type Parent Extends Object
Dim As Integer Ip
Declare Virtual Operator Let(Byref p As Parent)
End Type
Operator Parent.Let(Byref p As Parent)
This.Ip = p.Ip
Print " (lhs).Ip = (rhs).Ip"
End Operator
Type Child Extends Parent
Dim As Integer Ic
Declare Virtual Operator Let(Byref p As Parent)
End Type
Operator Child.Let(Byref p As Parent)
' Cast(Parent, This) = p '' NOK : infinite loop
Procptr(Parent.Let)(This, p) '' OK
If p Is Child Then
This.Ic = Cptr(Child Ptr, @p)->Ic
Print " (lhs).Ic = (rhs).Ic"
End If
End Operator
Dim As Parent p1, p2
Dim As Child c1, c2 '' for inheritance tests
Dim Byref As Parent rpc1 = c1, rpc2 = c2 '' for polymorphism tests
Print "'p1 = p2' :"
p1 = p2
Print
Print "'c1 = c2' :"
c1 = c2
Print
Print "'rpc1 = rpc2' :"
rpc1 = rpc2
Print
Print "'rpc1 = c2' :"
rpc1 = c2
Print
Print "'c1 = rpc2' :"
c1 = rpc2
Print
Sleep
Code: Select all
'p1 = p2' : (lhs).Ip = (rhs).Ip 'c1 = c2' : (lhs).Ip = (rhs).Ip (lhs).Ic = (rhs).Ic 'rpc1 = rpc2' : (lhs).Ip = (rhs).Ip (lhs).Ic = (rhs).Ic 'rpc1 = c2' : (lhs).Ip = (rhs).Ip (lhs).Ic = (rhs).Ic 'c1 = rpc2' : (lhs).Ip = (rhs).Ip (lhs).Ic = (rhs).Ic