Author Topic: Inheritance via Pointers  (Read 3682 times)

0 Members and 1 Guest are viewing this topic.

Offline rdc

  • Pentium
  • *****
  • Posts: 1495
  • Karma: 140
  • Yes, it is me.
    • View Profile
    • Clark Productions
Inheritance via Pointers
« on: April 28, 2011 »
I saw this technique in the book C++ Pointers and Dynamic Memory Management and thought it was very cool, so I thought I would see if this worked in FB. It seems to.

Code: [Select]
'Inheritance using pointers.

Type base
an As ZString * 20
a As Integer
End Type

Type dr1
inherit As base
b As Integer
End Type

Type dr2
inherit2 As dr1
c As Integer
End Type

Sub PrintBase(b As base Ptr)
Print "an (base): " & b->an
Print "a (base): " & b->a
Print
End Sub

Sub PrintD1(d1 As dr1 Ptr)
Print "an (base): " & CPtr(base Ptr, d1)->an
Print "a (base): " & CPtr(base Ptr, d1)->a
Print "b (dr1):" & d1->b
Print
End Sub

Sub PrintD2(d2 As dr2 Ptr)
Print "an (base): " & CPtr(base Ptr, d2)->an
Print "a (base): " & CPtr(base Ptr, d2)->a
Print "b (dr1): " & CPtr(dr1 Ptr, d2)->b
Print "c (dr2): " & d2->c
Print
End Sub

Dim dp2 As dr2 Ptr = New dr2
Dim dp1 As dr1 Ptr = CPtr(dr1 Ptr, dp2)
Dim bp As base Ptr = CPtr(base Ptr, dp2)

'Set the base values.
bp->an = "Hello World!"
bp->a = 20
'Set the d1 value.
dp1->b = 30
'Set the d2 value.
dp2->c = 40

'Print the values.
PrintBase bp
PrintD1 dp1
PrintD2 dp2
Sleep

Delete dp2

Output:

Quote
an (base): Hello World!
a (base): 20

an (base): Hello World!
a (base): 20
b (dr1):30

an (base): Hello World!
a (base): 20
b (dr1): 30
c (dr2): 40

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
Re: Inheritance via Pointers
« Reply #1 on: April 29, 2011 »
very cool rdc karma+ for teaching me something new.

getting freebasic to work in a more cpp way gets my thumbs up, i will be trying some stuff out with this now.

cheers mate.
Challenge Trophies Won:

Offline rdc

  • Pentium
  • *****
  • Posts: 1495
  • Karma: 140
  • Yes, it is me.
    • View Profile
    • Clark Productions
Re: Inheritance via Pointers
« Reply #2 on: April 29, 2011 »
Thanks.

It is interesting that according to the research I have done on this, C++ implements inheritance in a similar manner. That was actually the point of the code in the book; showing that inheritance is really just a set of pointers that point to the different class (struct) definitions. You just don't have to use the cast operator since the compiler does that for you.

Until inheritance finally makes it into FreeBasic, I think this could be useful.

Offline rdc

  • Pentium
  • *****
  • Posts: 1495
  • Karma: 140
  • Yes, it is me.
    • View Profile
    • Clark Productions
Re: Inheritance via Pointers
« Reply #3 on: April 29, 2011 »
Adding more OOP stuff:

Code: [Select]
'Inheritance using pointers.

Type base
   Private:
an As ZString * 20
a As Integer
Public:
Declare Constructor ()
Declare Destructor ()
Declare Property anGet () As String
Declare Property aGet() As Integer
End Type

Constructor base ()
   Print "Constructor base"
   an = "Hello World!"
   a = 20
End Constructor

Destructor base ()
   Print "Destructor base"
End Destructor

Property base.anGet () As String
   Return an
End Property

Property base.aGet() As Integer
   Return a
End Property

Type dr1
inherit As base
b As Integer
End Type

Type dr2
inherit2 As dr1
c As Integer
End Type

Sub PrintBase(b As base Ptr)
Print "an (base): " & b->anGet
Print "a (base): " & b->aGet
Print
End Sub

Sub PrintD1(d1 As dr1 Ptr)
Print "an (base): " & CPtr(base Ptr, d1)->anGet
Print "a (base): " & CPtr(base Ptr, d1)->aGet
Print "b (dr1):" & d1->b
Print
End Sub

Sub PrintD2(d2 As dr2 Ptr)
Print "an (base): " & CPtr(base Ptr, d2)->anGet
Print "a (base): " & CPtr(base Ptr, d2)->aGet
Print "b (dr1): " & CPtr(dr1 Ptr, d2)->b
Print "c (dr2): " & d2->c
Print
End Sub

Dim dp2 As dr2 Ptr = New dr2
Dim dp1 As dr1 Ptr = CPtr(dr1 Ptr, dp2)
Dim bp As base Ptr = CPtr(base Ptr, dp2)

'Set the base values.
'bp->an = "Hello World!"
'bp->a = 20
'Set the d2 value.
dp1->b = 30
'Set the d1 value.
dp2->c = 40

'Print the values.
PrintBase bp
PrintD1 dp1
PrintD2 dp2
Delete dp2
Sleep

Output:

Quote
Constructor base
an (base): Hello World!
a (base): 20

an (base): Hello World!
a (base): 20
b (dr1):30

an (base): Hello World!
a (base): 20
b (dr1): 30
c (dr2): 40

Destructor base