Another simple case...
Class ClonableBase
Protected fMyProp As Integer
Public Sub MyProp(Assigns i As Integer)
fMyProp = i
End Sub
Public Function MyProp() As Integer
Return fMyProp
End Sub
Public Sub ClonableBase()
// This isn't strictly necessary as RB initializes variables to a
0-equivalent, but I'm
// retentive that way...
fMyProp = 0
End Sub
Public Sub ClonableBase(o As ClonableBase)
If (o <> nil) Then
Me.MyProp = o.MyProp
Else
// Do something bad...
Raise New NilObjectException
End If
End Sub
Public Function Clone()
Dim result As ClonableBase
result = New ClonableBase(Me)
Return result
End Function
End Class
simple enough. Now make a subclass of ClonableBase:
Class ClonableSub : ClonableBase
Protected fMyOtherProp As String
Public Sub MyOtherProp(Assigns s As string)
fMyOtherProp = Left(s, 1) // We only want 1 character
End Sub
Public Function MyOtherProp() As String
Return fMyOtherProp
End Sub
Public Sub ClonableSub()
Super.ClonableBase()
fMyOtherProp = "Y"
End Sub
Public Sub ClonableSub(o As ClonableSub)
Dim temp As ClonableBase
temp = ClonableBase(o)
Super.ClonableBase(temp)
Me.MyOtherProp = o.MyOtherProp
End Sub
Public Function Clone()
Dim result As ClonableSub
result = New ClonableSub(Me)
Return result
End Sub
End Class
This should - I hope - perform a 'deep copy'; a copy that produces a
separate object whose properties match those of the cloned object
(given either a ClonableBase or ClonableSub instance, and a call to its
Clone() method).
Question to RB OOP gurus: Is this the 'safe' way to do it, or will
polymorphism throw me for a loop if I'm not careful, when calling it
from base class references (in an array, for example)? i.e. what will
happen in the following code?
Dim theObjects(2) As ClonableBase
Dim theBaseObject As ClonableBase
Dim theSubObject As ClonableSub
theBaseObject = New ClonableBase()
theBaseObject.MyProp = 5 // just as a test
theSubObject = New ClonableSub()
theSubObject.MyOtherProp = "N" // 'nuther test
theObjects(0) = theBaseObject
theObjects(11) = theSubObject
....
// other code
....
theBaseObject = theObjects(1).Clone() // ??? what will I really get as
the runtime type of 'theBaseObject'?
theSubObject = theObjects(1).Clone() // ??? is this legal?
theSubObject = ClonableSub(theObjects(1)).Clone // ??? is this legal?
William H Squires Jr
4400 Horizon Hill #4006
San Antonio, TX 78229
wsquires at satx dot rr dot com dot nospam <- remove the .nospam
_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>
Search the archives of this list here:
<http://support.realsoftware.com/listarchives/lists.html>
|