|
So I'm quite comfortable with shallow and deep cloning for value and reference types but want to check how this plays out for an array of a value type.
I have a 2D array of double and since the array contains value types I think the Array.Clone method (whilst only guaranteeing shallow copies) will give a deep copy in this instance. Am I right?
Private _matrix as double(,)
Public Function CopyToArray() as Double(,)
Return Me._matrix.Clone()
End Fucntion
Public Function Copy() As Matrix
Return New Matrix(Me.CopyToArray())
End Function
Public Sub New(ByVal array As Double(,))
Me._matrix = array
End Sub
The constructor assumes that the user wants to assign the 2D array of double(,) they have already created to the new matrix instance and hence doesn't use Array.Clone (and since the array is a class and is therefore a reference type, ByVal essentially has no meaning in this context). Did I get that bit right?
Thanks,
Mike
|
|
|
|
|
It will copy the values. Now tell me, is that shallow or deep?
Bastard Programmer from Hell
if you can't read my code, try converting it here[^]
|
|
|
|
|
Eddy Vluggen wrote: Now tell me, is that shallow or deep?
Taking the question seiously for a moment, for copy/clone of value types or arrays of a value type, shallow == deep. For reference types, copy/clone shallow != deep.
So, last question, since an array is a reference type then there's a vast difference between these two alternatives:
Private _matrix as double(,)
Public Sub New(ByVal array As Double(,))
Me._matrix = array
End Sub
and
Private _matrix as double(,)
Public Sub New(ByVal array As Double(,))
Me._matrix = array.Clone
End Sub
Whereas with an array of reference types there would only be a subtle difference?
Mike
|
|
|
|
|
Mike-MadBadger wrote: for copy/clone of value types or arrays of a value type, shallow == deep
Mike-MadBadger wrote:
Private _matrix as double(,)
Public Sub New(ByVal array As Double(,))
Me._matrix = array
End Sub
Would assign the pointer to the array to "Me._matrix". (Both in case of value- and reference types)
Mike-MadBadger wrote:
Private _matrix as double(,)
Public Sub New(ByVal array As Double(,))
Me._matrix = array.Clone
End Sub
Would create a copy of the values (shallow, again for value and reference-types)
Mike-MadBadger wrote: Whereas with an array of reference types there would only be a subtle difference?
In the case of value-types, the result would be identical; you'd end up with an array of values. For reference-types, it'd be similar; version one assigns a pointer to the array, version two copies the structure of the array (and thus, all pointers to references it contains) and you end up with an exact copy of the list, still pointing to the original objects.
A shallow copy of an Array copies only the elements of the Array, whether they are reference types or value types, but it does not copy the objects that the references refer to. The references in the new Array point to the same objects that the references in the original Array point to.
Bastard Programmer from Hell
if you can't read my code, try converting it here[^]
|
|
|
|
|
Apologies for the delay, been busy leaving work ready to move to a new country !!
I think we are absolutely agreeing but saying it in different ways, so for clarity I wanted to check this comment:
Eddy Vluggen wrote: In the case of value-types, the result would be identical; you'd end up with an array of values.
I don't understand the identical bit. In the first case you have a pointer to an existing instance of an array in the second case you create a new instance of an array with the same structure as the one you cloned? Perhaps pedantry but in this case, at least for understanding, forgiveable pedantry I feel...
That's what leads to clarity over the value / reference difference.
In the value case the new array structure contains new, independent value 'objects'.
Whereas in the reference case the new array structure contains new, independent pointers to the same object instances that the pointers in the original array pointed to.
Mike
|
|
|
|
|
Mike-MadBadger wrote: been busy leaving work ready to move to a new country !!
Getting ready for adventure? From where to where?
Mike-MadBadger wrote: I don't understand the identical bit. [..] Perhaps pedantry but in this case, at least for understanding, forgiveable pedantry I feel...
It's not pedantry, but being precise.
Mike-MadBadger wrote: In the first case you have a pointer to an existing instance of an array in the second case you create a new instance of an array with the same structure as the one you cloned?
Exactly
Bastard Programmer from Hell
if you can't read my code, try converting it here[^]
|
|
|
|
|
Eddy Vluggen wrote: Getting ready for adventure? From where to where?
UK (Reading) to France (Lyon).
Same industry, different company, family et al in tow. It certainly will be an adventure. Thanks for asking.
|
|
|
|
|
Good luck and enjoy
|
|
|
|
|
As to whether that difference is *important*... well, that completely depends on what you do with them.
Say you have a User object and this involves the concept of a list of Favourites (products, girls, doesn't matter). It'd probably be a List<Favourite> rather than a Favourite[], but no matter. The point is if for some reason you wanted to let one user copy another user's favourites, what should you do? Just point to the same list? Or make an actual copy of the list? Surely you should copy it, that way if user A subsequently edits his copy, the other one is not affected.
What about the Product objects themselves? Well, there's no need to clone them unless they "belong" to the user. If they are shared, you could happily point to the same Products, and both users would see any changes to products they have in their respective lists.
--
I know this isn't the right forum for it, but I gotta ask: Are you really from Hell, Eddy? A lot of English speakers claim they're from Hell, but most of them don't even know where it is! Look up "Hell, Stjørdal, Norway" on Google Maps, and you won't be one of them.
It depends. It *always* depends.
|
|
|
|
|
dojohansen wrote: The point is if for some reason you wanted to let one user copy another user's favourites, what should you do? Just point to the same list?
I'd serialize the list, as the user won't be sharing my PC's memory The most obvious route would be XML.
dojohansen wrote: I know this isn't the right forum for it, but I gotta ask: Are you really from Hell, Eddy?
It *depends*; if you mean the physical Hell from Norway, I hope to spend my old age there. Not only is it a very low-populated area, one also gets a 6-month night! I heard that one can even get there by train, and internet can probably be arranged too
Bastard Programmer from Hell
if you can't read my code, try converting it here[^]
|
|
|
|
|