I had prepared a series of examples for using 'New ([])'/'Delete ([])' in a polymorphic context, but like the proposed article "
17. How to Use Implicit / Overload New ( []) and Delete ([]) FB Operators with Simple UDT up to Subtype Polymorphism" will not be written (see the discussion about that from post
viewtopic.php?p=250918#p250918), it is still better to store the examples into this very technical topic.
1) Context
Structure hierarchy:
- base Type: Animal
- derived Types: Cat, Dog
Data fields:
- a string ('name') in Animal
- a string ('favorite) in Cat and in Dog
Procedure fields:
- an abstract/virtual sub: 'Init (Byref _name As String, Byref _favorite As String)'
- an abstract/virtual function: 'get_attributes() As String'
- a virtual destructor: 'Destructor ()'
in addition for 'New[]'/'Delete[]' usage:
- an abstract/virtual [] operator: 'Operator [] (Byval _n As Integer) Byref As Animal/Cat/Dog' (covariant Byref return)
- an abstract/virtual sub: 'DeleteSB_Launcher ()'
4 objects constructed:
- 2 instances of Cat
- 2 instances of Dog
8 contexts of code:
- 4 for 'New'/'Delete': using base pointers, or base references, or base pointer array, or base reference array
- 4 for 'New[]'/'Delete[]': using base pointers, or base references, or base pointer array, or base reference array
(a macro simulates the reference fix-len array feature)
2) Examples
- All 8 codes output the same text (if working) on console:
Code: Select all
Cat constructor
Cat constructor
Dog constructor
Dog constructor
Tiger: Cat Meow Salmon
Kitty: Cat Meow Sardine
Buddy: Dog Woof Lamb
Molly: Dog Woof Beef
Cat destructor
Cat destructor
Dog destructor
Dog destructor
- Code for 'New'/'Delete' and using base pointers in a polymorphic context:
Code: Select all
Type Animal Extends Object
Public:
Declare Abstract Function get_attributes() As String
Declare Abstract Sub Init (Byref _name As String, Byref _favorite As String)
Declare Virtual Destructor ()
Protected:
Dim As String name
Declare Constructor ()
Private:
Declare Constructor (Byref _a As Animal)
End Type
Destructor Animal ()
End Destructor
Constructor Animal ()
End Constructor
Type Cat Extends Animal
Public:
Declare Constructor ()
Declare Virtual Sub Init (Byref _name As String, Byref _favorite As String)
Declare Virtual Function get_attributes() As String
Declare Virtual Destructor ()
Private:
Dim As String favorite
End Type
Constructor Cat ()
Print "Cat constructor"
End Constructor
Sub Cat.Init (Byref _name As String, Byref _favorite As String = "")
This.name = _name
This.favorite = _favorite
End Sub
Destructor Cat ()
Print "Cat destructor"
End destructor
Function Cat.get_attributes() As String
Return This.name & ": Cat Meow " & This.favorite
End Function
Type Dog Extends Animal
Public:
Declare Constructor ()
Declare Virtual Sub Init (Byref _name As String, Byref _favorite As String)
Declare Virtual Function get_attributes() As String
Declare Virtual Destructor ()
Private:
Dim As String favorite
End Type
Constructor Dog ()
Print "Dog constructor"
End Constructor
Sub Dog.Init (Byref _name As String, Byref _favorite As String)
This.name = _name
This.favorite = _favorite
End Sub
Destructor Dog ()
Print "Dog destructor"
End destructor
Function Dog.get_attributes() As String
Return This.name & ": Dog Woof " & This.favorite
End Function
'------------------------------------------------------------------------------
Dim As Animal Ptr pac1 = New Cat()
Dim As Animal Ptr pac2 = New Cat()
Dim As Animal Ptr pad1 = New Dog()
Dim As Animal Ptr pad2 = New Dog()
pac1->Init("Tiger", "Salmon")
pac2->Init("Kitty", "Sardine")
pad1->Init("Buddy", "Lamb")
pad2->Init("Molly", "Beef")
Print " " & pac1->get_attributes()
Print " " & pac2->get_attributes()
Print " " & pad1->get_attributes()
Print " " & pad2->get_attributes()
Delete pac1
Delete pac2
Delete pad1
Delete pad2
Sleep
- Code for 'New'/'Delete' and using base references in a polymorphic context:
Code: Select all
Type Animal Extends Object
Public:
Declare Abstract Function get_attributes() As String
Declare Abstract Sub Init (Byref _name As String, Byref _favorite As String)
Declare Virtual Destructor ()
Protected:
Dim As String name
Declare Constructor ()
Private:
Declare Constructor (Byref _a As Animal)
End Type
Destructor Animal ()
End Destructor
Constructor Animal ()
End Constructor
Type Cat Extends Animal
Public:
Declare Constructor ()
Declare Virtual Sub Init (Byref _name As String, Byref _favorite As String)
Declare Virtual Function get_attributes() As String
Declare Virtual Destructor ()
Private:
Dim As String favorite
End Type
Constructor Cat ()
Print "Cat constructor"
End Constructor
Sub Cat.Init (Byref _name As String, Byref _favorite As String = "")
This.name = _name
This.favorite = _favorite
End Sub
Destructor Cat ()
Print "Cat destructor"
End destructor
Function Cat.get_attributes() As String
Return This.name & ": Cat Meow " & This.favorite
End Function
Type Dog Extends Animal
Public:
Declare Constructor ()
Declare Virtual Sub Init (Byref _name As String, Byref _favorite As String)
Declare Virtual Function get_attributes() As String
Declare Virtual Destructor ()
Private:
Dim As String favorite
End Type
Constructor Dog ()
Print "Dog constructor"
End Constructor
Sub Dog.Init (Byref _name As String, Byref _favorite As String)
This.name = _name
This.favorite = _favorite
End Sub
Destructor Dog ()
Print "Dog destructor"
End destructor
Function Dog.get_attributes() As String
Return This.name & ": Dog Woof " & This.favorite
End Function
'------------------------------------------------------------------------------
Dim Byref As Animal rac1 = *New Cat()
Dim Byref As Animal rac2 = *New Cat()
Dim Byref As Animal rad1 = *New Dog()
Dim Byref As Animal rad2 = *New Dog()
rac1.Init("Tiger", "Salmon")
rac2.Init("Kitty", "Sardine")
rad1.Init("Buddy", "Lamb")
rad2.Init("Molly", "Beef")
Print " " & rac1.get_attributes()
Print " " & rac2.get_attributes()
Print " " & rad1.get_attributes()
Print " " & rad2.get_attributes()
Delete @rac1
Delete @rac2
Delete @rad1
Delete @rad2
Sleep
- Code for 'New'/'Delete' and using base pointer array in a polymorphic context:
Code: Select all
Type Animal Extends Object
Public:
Declare Abstract Function get_attributes() As String
Declare Abstract Sub Init (Byref _name As String, Byref _favorite As String)
Declare Virtual Destructor ()
Protected:
Dim As String name
Declare Constructor ()
Private:
Declare Constructor (Byref _a As Animal)
End Type
Destructor Animal ()
End Destructor
Constructor Animal ()
End Constructor
Type Cat Extends Animal
Public:
Declare Constructor ()
Declare Virtual Sub Init (Byref _name As String, Byref _favorite As String)
Declare Virtual Function get_attributes() As String
Declare Virtual Destructor ()
Private:
Dim As String favorite
End Type
Constructor Cat ()
Print "Cat constructor"
End Constructor
Sub Cat.Init (Byref _name As String, Byref _favorite As String = "")
This.name = _name
This.favorite = _favorite
End Sub
Destructor Cat ()
Print "Cat destructor"
End destructor
Function Cat.get_attributes() As String
Return This.name & ": Cat Meow " & This.favorite
End Function
Type Dog Extends Animal
Public:
Declare Constructor ()
Declare Virtual Sub Init (Byref _name As String, Byref _favorite As String)
Declare Virtual Function get_attributes() As String
Declare Virtual Destructor ()
Private:
Dim As String favorite
End Type
Constructor Dog ()
Print "Dog constructor"
End Constructor
Sub Dog.Init (Byref _name As String, Byref _favorite As String)
This.name = _name
This.favorite = _favorite
End Sub
Destructor Dog ()
Print "Dog destructor"
End destructor
Function Dog.get_attributes() As String
Return This.name & ": Dog Woof " & This.favorite
End Function
'------------------------------------------------------------------------------
Dim As Animal Ptr pa(0 To ...) = {New Cat(), New Cat(), New Dog(), New Dog()}
pa(0)->Init("Tiger", "Salmon")
pa(1)->Init("Kitty", "Sardine")
pa(2)->Init("Buddy", "Lamb")
pa(3)->Init("Molly", "Beef")
For I As Integer = Lbound(pa) To Ubound(pa)
Print " " & pa(I)->get_attributes()
Next I
For I As Integer = Lbound(pa) To Ubound(pa)
Delete pa(I)
Next I
Sleep
- Code for 'New'/'Delete' and using base reference array in a polymorphic context:
Code: Select all
Type Animal Extends Object
Public:
Declare Abstract Function get_attributes() As String
Declare Abstract Sub Init (Byref _name As String, Byref _favorite As String)
Declare Virtual Destructor ()
Protected:
Dim As String name
Declare Constructor ()
Private:
Declare Constructor (Byref _a As Animal)
End Type
Destructor Animal ()
End Destructor
Constructor Animal ()
End Constructor
Type Cat Extends Animal
Public:
Declare Constructor ()
Declare Virtual Sub Init (Byref _name As String, Byref _favorite As String)
Declare Virtual Function get_attributes() As String
Declare Virtual Destructor ()
Private:
Dim As String favorite
End Type
Constructor Cat ()
Print "Cat constructor"
End Constructor
Sub Cat.Init (Byref _name As String, Byref _favorite As String = "")
This.name = _name
This.favorite = _favorite
End Sub
Destructor Cat ()
Print "Cat destructor"
End destructor
Function Cat.get_attributes() As String
Return This.name & ": Cat Meow " & This.favorite
End Function
Type Dog Extends Animal
Public:
Declare Constructor ()
Declare Virtual Sub Init (Byref _name As String, Byref _favorite As String)
Declare Virtual Function get_attributes() As String
Declare Virtual Destructor ()
Private:
Dim As String favorite
End Type
Constructor Dog ()
Print "Dog constructor"
End Constructor
Sub Dog.Init (Byref _name As String, Byref _favorite As String)
This.name = _name
This.favorite = _favorite
End Sub
Destructor Dog ()
Print "Dog destructor"
End destructor
Function Dog.get_attributes() As String
Return This.name & ": Dog Woof " & This.favorite
End Function
'------------------------------------------------------------------------------
'Macro simulating the declaration with initialization of a fixed-length array of references
'(element-name: 'refName(I)', array-name: 'refName_Array')
#macro DimByRefFixLenArrayInit (typeName, refName, lowBound, objectAddressList...)
Dim As typeName Ptr ptr##refName(lowBound to ...) = { objectAddressList }
#Define refName(n) (*ptr##refName(n))
#Define refName##_Array ptr##refName
#endmacro
'Dim Byref As Animal ra(0 To ...) = {*New Cat(), *New Cat(), *New Dog(), *New Dog()}
DimByRefFixLenArrayInit(Animal, ra, 0, New Cat(), New Cat(), New Dog(), New Dog())
ra(0).Init("Tiger", "Salmon")
ra(1).Init("Kitty", "Sardine")
ra(2).Init("Buddy", "Lamb")
ra(3).Init("Molly", "Beef")
For I As Integer = Lbound(ra_Array) To Ubound(ra_Array)
Print " " & ra(I).get_attributes()
Next I
For I As Integer = Lbound(ra_Array) To Ubound(ra_Array)
Delete @ra(I)
Next I
Sleep
- Code for 'New[]'/'Delete[]' and using base pointers in a polymorphic context:
Code: Select all
Type Animal Extends Object
Public:
Declare Abstract Function get_attributes() As String
Declare Abstract Sub Init (Byref _name As String, Byref _favorite As String)
Declare Virtual Destructor ()
Declare Abstract Operator [] (Byval _n As Integer) Byref As Animal
Declare Abstract Sub DeleteSB_Launcher ()
Protected:
Dim As String name
Declare Constructor ()
Private:
Declare Constructor (Byref _a As Animal)
End Type
Destructor Animal ()
End Destructor
Constructor Animal ()
End Constructor
Type Cat Extends Animal
Public:
Declare Constructor ()
Declare Virtual Sub Init (Byref _name As String, Byref _favorite As String)
Declare Virtual Function get_attributes() As String
Declare Virtual Destructor ()
Declare Virtual Operator [] (Byval _n As Integer) Byref As Cat
Declare Virtual Sub DeleteSB_Launcher ()
Private:
Dim As String favorite
End Type
Constructor Cat ()
Print "Cat constructor"
End Constructor
Sub Cat.Init (Byref _name As String, Byref _favorite As String = "")
This.name = _name
This.favorite = _favorite
End Sub
Destructor Cat ()
Print "Cat destructor"
End destructor
Function Cat.get_attributes() As String
Return This.name & ": Cat Meow " & This.favorite
End Function
Operator Cat.[] (Byval _n As Integer) Byref As Cat
Return (@This)[_n]
End Operator
Sub Cat.DeleteSB_Launcher ()
Delete[] @This
End Sub
Type Dog Extends Animal
Public:
Declare Constructor ()
Declare Virtual Sub Init (Byref _name As String, Byref _favorite As String)
Declare Virtual Function get_attributes() As String
Declare Virtual Destructor ()
Declare Virtual Operator [] (Byval _n As Integer) Byref As Dog
Declare Virtual Sub DeleteSB_Launcher ()
Private:
Dim As String favorite
End Type
Constructor Dog ()
Print "Dog constructor"
End Constructor
Sub Dog.Init (Byref _name As String, Byref _favorite As String)
This.name = _name
This.favorite = _favorite
End Sub
Destructor Dog ()
Print "Dog destructor"
End destructor
Function Dog.get_attributes() As String
Return This.name & ": Dog Woof " & This.favorite
End Function
Operator Dog.[] (Byval _n As Integer) Byref As Dog
Return (@This)[_n]
End Operator
Sub Dog.DeleteSB_Launcher ()
Delete[] @This
End Sub
'------------------------------------------------------------------------------
Dim As Animal Ptr pac = New Cat[2]
Dim As Animal Ptr pad = New Dog[2]
(*pac)[0].Init("Tiger", "Salmon")
(*pac)[1].Init("Kitty", "Sardine")
(*pad)[0].Init("Buddy", "Lamb")
(*pad)[1].Init("Molly", "Beef")
For I As Integer = 0 To 1
Print " " & (*pac)[I].get_attributes()
Next I
For I As Integer = 0 To 1
Print " " & (*pad)[I].get_attributes()
Next I
pac->DeleteSB_Launcher()
pad->DeleteSB_Launcher()
Sleep
- Code for 'New[]'/'Delete[]' and using base references in a polymorphic context:
Code: Select all
Type Animal Extends Object
Public:
Declare Abstract Function get_attributes() As String
Declare Abstract Sub Init (Byref _name As String, Byref _favorite As String)
Declare Virtual Destructor ()
Declare Abstract Operator [] (Byval _n As Integer) Byref As Animal
Declare Abstract Sub DeleteSB_Launcher ()
Protected:
Dim As String name
Declare Constructor ()
Private:
Declare Constructor (Byref _a As Animal)
End Type
Destructor Animal ()
End Destructor
Constructor Animal ()
End Constructor
Type Cat Extends Animal
Public:
Declare Constructor ()
Declare Virtual Sub Init (Byref _name As String, Byref _favorite As String)
Declare Virtual Function get_attributes() As String
Declare Virtual Destructor ()
Declare Virtual Operator [] (Byval _n As Integer) Byref As Cat
Declare Virtual Sub DeleteSB_Launcher ()
Private:
Dim As String favorite
End Type
Constructor Cat ()
Print "Cat constructor"
End Constructor
Sub Cat.Init (Byref _name As String, Byref _favorite As String = "")
This.name = _name
This.favorite = _favorite
End Sub
Destructor Cat ()
Print "Cat destructor"
End destructor
Function Cat.get_attributes() As String
Return This.name & ": Cat Meow " & This.favorite
End Function
Operator Cat.[] (Byval _n As Integer) Byref As Cat
Return (@This)[_n]
End Operator
Sub Cat.DeleteSB_Launcher ()
Delete[] @This
End Sub
Type Dog Extends Animal
Public:
Declare Constructor ()
Declare Virtual Sub Init (Byref _name As String, Byref _favorite As String)
Declare Virtual Function get_attributes() As String
Declare Virtual Destructor ()
Declare Virtual Operator [] (Byval _n As Integer) Byref As Dog
Declare Virtual Sub DeleteSB_Launcher ()
Private:
Dim As String favorite
End Type
Constructor Dog ()
Print "Dog constructor"
End Constructor
Sub Dog.Init (Byref _name As String, Byref _favorite As String)
This.name = _name
This.favorite = _favorite
End Sub
Destructor Dog ()
Print "Dog destructor"
End destructor
Function Dog.get_attributes() As String
Return This.name & ": Dog Woof " & This.favorite
End Function
Operator Dog.[] (Byval _n As Integer) Byref As Dog
Return (@This)[_n]
End Operator
Sub Dog.DeleteSB_Launcher ()
Delete[] @This
End Sub
'------------------------------------------------------------------------------
Dim Byref As Animal rac = *New Cat[2]
Dim Byref As Animal rad = *New Dog[2]
rac[0].Init("Tiger", "Salmon")
rac[1].Init("Kitty", "Sardine")
rad[0].Init("Buddy", "Lamb")
rad[1].Init("Molly", "Beef")
For I As Integer = 0 To 1
Print " " & rac[I].get_attributes()
Next I
For I As Integer = 0 To 1
Print " " & rad[I].get_attributes()
Next I
rac.DeleteSB_Launcher()
rad.DeleteSB_Launcher()
Sleep
- Code for 'New[]'/'Delete[]' and using base pointer array in a polymorphic context:
Code: Select all
Type Animal Extends Object
Public:
Declare Abstract Function get_attributes() As String
Declare Abstract Sub Init (Byref _name As String, Byref _favorite As String)
Declare Virtual Destructor ()
Declare Abstract Operator [] (Byval _n As Integer) Byref As Animal
Declare Abstract Sub DeleteSB_Launcher ()
Protected:
Dim As String name
Declare Constructor ()
Private:
Declare Constructor (Byref _a As Animal)
End Type
Destructor Animal ()
End Destructor
Constructor Animal ()
End Constructor
Type Cat Extends Animal
Public:
Declare Constructor ()
Declare Virtual Sub Init (Byref _name As String, Byref _favorite As String)
Declare Virtual Function get_attributes() As String
Declare Virtual Destructor ()
Declare Virtual Operator [] (Byval _n As Integer) Byref As Cat
Declare Virtual Sub DeleteSB_Launcher ()
Private:
Dim As String favorite
End Type
Constructor Cat ()
Print "Cat constructor"
End Constructor
Sub Cat.Init (Byref _name As String, Byref _favorite As String = "")
This.name = _name
This.favorite = _favorite
End Sub
Destructor Cat ()
Print "Cat destructor"
End destructor
Function Cat.get_attributes() As String
Return This.name & ": Cat Meow " & This.favorite
End Function
Operator Cat.[] (Byval _n As Integer) Byref As Cat
Return (@This)[_n]
End Operator
Sub Cat.DeleteSB_Launcher ()
Delete[] @This
End Sub
Type Dog Extends Animal
Public:
Declare Constructor ()
Declare Virtual Sub Init (Byref _name As String, Byref _favorite As String)
Declare Virtual Function get_attributes() As String
Declare Virtual Destructor ()
Declare Virtual Operator [] (Byval _n As Integer) Byref As Dog
Declare Virtual Sub DeleteSB_Launcher ()
Private:
Dim As String favorite
End Type
Constructor Dog ()
Print "Dog constructor"
End Constructor
Sub Dog.Init (Byref _name As String, Byref _favorite As String)
This.name = _name
This.favorite = _favorite
End Sub
Destructor Dog ()
Print "Dog destructor"
End destructor
Function Dog.get_attributes() As String
Return This.name & ": Dog Woof " & This.favorite
End Function
Operator Dog.[] (Byval _n As Integer) Byref As Dog
Return (@This)[_n]
End Operator
Sub Dog.DeleteSB_Launcher ()
Delete[] @This
End Sub
'------------------------------------------------------------------------------
Dim As Animal Ptr pa(0 To ...) = {New Cat[2], New Dog[2]}
(*pa(0))[0].Init("Tiger", "Salmon")
(*pa(0))[1].Init("Kitty", "Sardine")
(*pa(1))[0].Init("Buddy", "Lamb")
(*pa(1))[1].Init("Molly", "Beef")
For I As Integer = Lbound(pa) To Ubound(pa)
For J As Integer = 0 To 1
Print " " & (*pa(I))[J].get_attributes()
Next J
Next I
For I As Integer = Lbound(pa) To Ubound(pa)
pa(I)->DeleteSB_Launcher()
Next I
Sleep
- Code for 'New[]'/'Delete[]' and using base reference array in a polymorphic context:
Code: Select all
Type Animal Extends Object
Public:
Declare Abstract Function get_attributes() As String
Declare Abstract Sub Init (Byref _name As String, Byref _favorite As String)
Declare Virtual Destructor ()
Declare Abstract Operator [] (Byval _n As Integer) Byref As Animal
Declare Abstract Sub DeleteSB_Launcher ()
Protected:
Dim As String name
Declare Constructor ()
Private:
Declare Constructor (Byref _a As Animal)
End Type
Destructor Animal ()
End Destructor
Constructor Animal ()
End Constructor
Type Cat Extends Animal
Public:
Declare Constructor ()
Declare Virtual Sub Init (Byref _name As String, Byref _favorite As String)
Declare Virtual Function get_attributes() As String
Declare Virtual Destructor ()
Declare Virtual Operator [] (Byval _n As Integer) Byref As Cat
Declare Virtual Sub DeleteSB_Launcher ()
Private:
Dim As String favorite
End Type
Constructor Cat ()
Print "Cat constructor"
End Constructor
Sub Cat.Init (Byref _name As String, Byref _favorite As String = "")
This.name = _name
This.favorite = _favorite
End Sub
Destructor Cat ()
Print "Cat destructor"
End destructor
Function Cat.get_attributes() As String
Return This.name & ": Cat Meow " & This.favorite
End Function
Operator Cat.[] (Byval _n As Integer) Byref As Cat
Return (@This)[_n]
End Operator
Sub Cat.DeleteSB_Launcher ()
Delete[] @This
End Sub
Type Dog Extends Animal
Public:
Declare Constructor ()
Declare Virtual Sub Init (Byref _name As String, Byref _favorite As String)
Declare Virtual Function get_attributes() As String
Declare Virtual Destructor ()
Declare Virtual Operator [] (Byval _n As Integer) Byref As Dog
Declare Virtual Sub DeleteSB_Launcher ()
Private:
Dim As String favorite
End Type
Constructor Dog ()
Print "Dog constructor"
End Constructor
Sub Dog.Init (Byref _name As String, Byref _favorite As String)
This.name = _name
This.favorite = _favorite
End Sub
Destructor Dog ()
Print "Dog destructor"
End destructor
Function Dog.get_attributes() As String
Return This.name & ": Dog Woof " & This.favorite
End Function
Operator Dog.[] (Byval _n As Integer) Byref As Dog
Return (@This)[_n]
End Operator
Sub Dog.DeleteSB_Launcher ()
Delete[] @This
End Sub
'------------------------------------------------------------------------------
'Macro simulating the declaration with initialization of a fixed-length array of references
'(element-name: 'refName(I)', array-name: 'refName_Array')
#macro DimByRefFixLenArrayInit (typeName, refName, lowBound, objectAddressList...)
Dim As typeName Ptr ptr##refName(lowBound to ...) = { objectAddressList }
#Define refName(n) (*ptr##refName(n))
#Define refName##_Array ptr##refName
#endmacro
'Dim Byref As Animal ra(0 To ...) = {*New Cat[2], *New Dog[2]}
DimByRefFixLenArrayInit(Animal, ra, 0, New Cat[2], New Dog[2])
ra(0)[0].Init("Tiger", "Salmon")
ra(0)[1].Init("Kitty", "Sardine")
ra(1)[0].Init("Buddy", "Lamb")
ra(1)[1].Init("Molly", "Beef")
For I As Integer = Lbound(ra_Array) To Ubound(ra_Array)
For J As Integer = 0 To 1
Print " " & ra(I)[J].get_attributes()
Next J
Next I
For I As Integer = Lbound(ra_Array) To Ubound(ra_Array)
ra(I).DeleteSB_Launcher()
Next I
Sleep