New fbc branch ('inheritance') on sourceforge (fbc SVN)
How much is this used in C++ ? And then I mean both in general (any form of MI), and basic ( where only one ancestor contains code, and all others are abstract (like Java interfaces)) ?MOD wrote:@v1c: so far you've added single inheritance. But if you want to use C++ libs in future, multiple inheritance would be necessary too. Any plans on this?
I'm not sure how I should translate something like this without multiple inheritance:
orctrlsub.h from wxWidgets wrote:class WXDLLEXPORT wxControlWithItems : public wxControl, public wxItemContainer
sizer.h from wxWidgets wrote:class WXDLLEXPORT wxSizer: public wxObject, public wxClientDataContainer
Branch on sf.net (SVN): http://fbc.svn.sourceforge.net/viewvc/f ... heritance/
Compiled fbc with inheritance: http://www.freebasic.net/forum/viewtopi ... 572#156572
Compiled fbc with inheritance: http://www.freebasic.net/forum/viewtopi ... 572#156572
Emulation of multiple inheritance by means of a common super-class
UDT12 inherits from UDT1 and UDT2 by means of a common super-class UDT which allows to go down then go up to UDT1 and UDT2:
UDT12 inherits from UDT1 and UDT2 by means of a common super-class UDT which allows to go down then go up to UDT1 and UDT2:
Code: Select all
Type UDT ' common super-class
Dim NotUsed As Integer
Declare Sub Print1 ()
Declare Sub Print2 ()
End Type
Type UDT1 Extends UDT
Declare Sub Print1 ()
End Type
Sub UDT1.Print1 ()
Print "member procedure of class UDT1"
End Sub
Sub UDT.Print1 ()
Cast(UDT1 Ptr, @This)->Print1 ' just a relay to the member procedure Print1 of UDT1
End Sub
Type UDT2 Extends UDT
Declare Sub Print2 ()
End Type
Sub UDT2.Print2 ()
Print "member procedure of class UDT2"
End Sub
Sub UDT.Print2 ()
Cast(UDT2 Ptr, @This)->Print2 ' just a relay to the member procedure Print2 of UDT2
End Sub
'--------------------------------------------------------------------------
Type UDT12 Extends UDT
End Type
'==========================================================================
Dim test12ptr As UDT12 Ptr = New UDT12
test12ptr->Print1
test12ptr->Print2
Delete test12ptr
Print
Dim test12 As UDT12
test12.Print1
test12.Print2
Sleep
This can even be done entirely without extension. Aside, I'm not sure this helps my particular problem, it just makes my code more messy. But I will have to do a few tests before deciding. The idea is really good, I like it :-)
Cheers,
Mike
Cheers,
Mike
Code: Select all
Type UDT ' common super-class
Dim NotUsed As Integer
Declare Sub Print1 ()
Declare Sub Print2 ()
End Type
Type UDT1
Dim NotUsed As Integer
Declare Sub Print1 ()
End Type
Type UDT2
Dim NotUsed As Integer
Declare Sub Print2 ()
End Type
Sub UDT1.Print1 ()
Print "member procedure of class UDT1"
End Sub
Sub UDT2.Print2 ()
Print "member procedure of class UDT2"
End Sub
Sub UDT.Print1 ()
Cast(UDT1 Ptr, @This)->Print1 ' just a relay to the member procedure Print1 of UDT1
End Sub
Sub UDT.Print2 ()
Cast(UDT2 Ptr, @This)->Print2 ' just a relay to the member procedure Print2 of UDT2
End Sub
Dim testptr As UDT Ptr = New UDT
testptr->Print1
testptr->Print2
Delete testptr
Print
Dim test As UDT
test.Print1
test.Print2
Sleep
It's a workaround, like it was done with inheritance without the new branch, but it won't solve my problem with single inheritance.
Yes, It allows to easily call only static member procedure of another class!
But using inheritance, it is also possible to call, from the super-class UDT, a non-static member procedure PrintI of a sub-class UDTI, processing member data-field of this same super-class UDT:
- Member procedure inheritance usable also from any sub-class (in the opposite direction):
Dim test AS UDT
test.Print1
test.Print2
- Member procedure inheritance usable also between any sub-classes
Dim test1 As UDT1
test1.Print2
Dim test2 As UDT2
test2.Print1
Any member procedure of any sub-class can be called from any instance (of any sub-class or of super-class).
All the combinations are authorized:
But using inheritance, it is also possible to call, from the super-class UDT, a non-static member procedure PrintI of a sub-class UDTI, processing member data-field of this same super-class UDT:
- Member procedure inheritance usable also from any sub-class (in the opposite direction):
Dim test AS UDT
test.Print1
test.Print2
- Member procedure inheritance usable also between any sub-classes
Dim test1 As UDT1
test1.Print2
Dim test2 As UDT2
test2.Print1
Any member procedure of any sub-class can be called from any instance (of any sub-class or of super-class).
All the combinations are authorized:
Code: Select all
Type UDT ' common super-class
Dim Used1 As String = "processing member field Used1 of the super-class UDT."
Dim Used2 As String = "processing member field Used2 of the super-class UDT."
Declare Sub Print1 ()
Declare Sub Print2 ()
End Type
Type UDT1 Extends UDT
Declare Sub Print1 ()
End Type
Type UDT2 Extends UDT
Declare Sub Print2 ()
End Type
Sub UDT1.Print1 ()
Print "- member procedure Print1 of sub-class UDT1,"
Print This.Used1
End Sub
Sub UDT2.Print2 ()
Print "- member procedure Print2 of sub-class UDT2,"
Print This.Used2
End Sub
Sub UDT.Print1 ()
Cast(UDT1 Ptr, @This)->Print1 ' just a relay to the member procedure Print1 of UDT1
End Sub
Sub UDT.Print2 ()
Cast(UDT2 Ptr, @This)->Print2 ' just a relay to the member procedure Print2 of UDT2
End Sub
'--------------------------------------------------------------------------
Dim test AS UDT
Print "From super-class UDT instance:"
test.Print1
test.Print2
Print
Dim test1 As UDT1
Print "From sub-class UDT1 instance:"
test1.Print1
test1.Print2
Print
Dim test2 As UDT2
Print "From sub-class UDT2 instance:"
test2.Print1
test2.Print2
Print
Sleep
I thought especially in the interest for pure FreeBASIC:fxm wrote:Any member procedure of any sub-class can be called from any instance (of any sub-class or of super-class).
All the combinations are authorized.
With this specific structure of classes, procedure inheritance becomes omni-directional among sub-classes plus super-class.
( http://www.freebasic.net/forum/viewtopi ... 127#162127 )