New fbc branch ('inheritance') on sourceforge (fbc SVN)

General discussion for topics related to the FreeBASIC project or its community.
Post Reply
MOD
Posts: 555
Joined: Jun 11, 2009 20:15

Post by MOD »

@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?
marcov
Posts: 3456
Joined: Jun 16, 2005 9:45
Location: Netherlands
Contact:

Post by marcov »

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?
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)) ?
v1ctor
Site Admin
Posts: 3804
Joined: May 27, 2005 8:08
Location: SP / Bra[s]il
Contact:

Post by v1ctor »

No, there's no plan to support multiple inheritance.

The class memory layout and RTTI data are compatible with GCC, so in theory you could reuse the C++ libs compiled with GCC if the headers are translated.
MOD
Posts: 555
Joined: Jun 11, 2009 20:15

Post by MOD »

I'm not sure how I should translate something like this without multiple inheritance:
ctrlsub.h from wxWidgets wrote:class WXDLLEXPORT wxControlWithItems : public wxControl, public wxItemContainer
or
sizer.h from wxWidgets wrote:class WXDLLEXPORT wxSizer: public wxObject, public wxClientDataContainer
h4tt3n
Posts: 698
Joined: Oct 22, 2005 21:12
Location: Denmark

Post by h4tt3n »

Could someone please post a direct link to the inheritance branch? I'd really like to check it out but I can't seem to find it.
MOD
Posts: 555
Joined: Jun 11, 2009 20:15

Post by MOD »

h4tt3n
Posts: 698
Joined: Oct 22, 2005 21:12
Location: Denmark

Post by h4tt3n »

Thanks.
h4tt3n
Posts: 698
Joined: Oct 22, 2005 21:12
Location: Denmark

Post by h4tt3n »

Okay, tried it out and really like the possibillities. Unfortunately I fairly quickly ran into some walls due to the lack of multiple extensions, so I second MOD's request for this feature.

Cheers,
Mike
fxm
Moderator
Posts: 12098
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Post by fxm »

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:

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
h4tt3n
Posts: 698
Joined: Oct 22, 2005 21:12
Location: Denmark

Post by h4tt3n »

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

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
MOD
Posts: 555
Joined: Jun 11, 2009 20:15

Post by MOD »

It's a workaround, like it was done with inheritance without the new branch, but it won't solve my problem with single inheritance.
fxm
Moderator
Posts: 12098
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Post by fxm »

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:

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
MOD
Posts: 555
Joined: Jun 11, 2009 20:15

Post by MOD »

If you have only headerfiles of a lib you can't use this trick, so no compability to C++.
fxm
Moderator
Posts: 12098
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Post by fxm »

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.
I thought especially in the interest for pure FreeBASIC:
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 )
Post Reply