Re: Cloning and Distinct Copies of Objects





Hal Vaughan wrote On 08/15/06 14:40,:
Eric Sosman wrote:

You could *copy* (not *clone*) the String objects by using
String's copy constructor, or by a few other routes. But it's
a dumb thing to do: Why do you think you need all those copies?

I'm using a "restore to last saved" option in the menu so if someone makes
mistakes, they can revert to the last saved data. I need to store several
String[] structures as they are when the data is saved so they can be used
later if needed.

You may need a copy of the String[], which is an array of
references to String objects, but there's no need to make copies
of the String objects those references point to.

Here's the original array, before anybody edits anything:

oldarray[0] -> "Duke"
oldarray[1] -> "rulez!"

Now make a new copy of the array and the references it contains,
which point to the original String objects:

oldarray[0] -> "Duke" <- newarray[0]
oldarray[1] -> "rulez!" <- newarray[1]

Now edit the new array, changing one of its references to point
to a different String:

oldarray[0] -> "Duke" newarray[0] -> "Elvis"
oldarray[1] -> "rulez!" <- newarray[1]

There is no need for extra copies of "rulez!" or of "Duke".
All the String objects are still alive, still referred to,
still hanging on to their immutable content. If you want
to commit the edit, keep newarray and let oldarray drop on
the floor. If you want to revert, keep oldarray and abandon
newarray to the garbage collector.

I think the distinction between an object reference and
an object instance is not yet quite clear to you. When the
light eventually dawns, things will make sense quite suddenly.

Objects are telephones; references are telephone numbers.
If you want to get calls from a hundred people, give out a
hundred copies of your telephone number -- a hundred copies
of the reference value -- but don't burden yourself by trying
to carry a hundred phones on your belt.

--
Eric.Sosman@xxxxxxx

.



Relevant Pages

  • Re: Test if memory pointer is valid?
    ... I can see setting a new pointer initially to the address of the beginning of an array, for example, and then incrementing the pointer to address subsequent elements, but that would usually be done inside a function, where the new pointer would go out of scope at the end of the function and be reinitialized if it is called again. ... I would like to see a specific example where it is necessary to have multiple references such as this where it would be necessary to check for it having been freed. ... copies of all the TLabel references already in the form as individual ...
    (comp.lang.pascal.delphi.misc)
  • Re: UBound not in Intellisense. References problem?
    ... but you can produce an array by using Value ... I wouldn't worry about it not appearing in intellisense. ... > I wonder if I have a corruption in my registration of References, ... I get UCase, UCase$, but no UBound. ...
    (microsoft.public.excel.programming)
  • Re: How come Ada isnt more popular?
    ... The language does not require array implementation to be contiguous. ... then But non contigous representation of arrays will really stress ... It's easy with value-oriented languages (i.e. languages ... use references because of that. ...
    (comp.lang.ada)
  • Re: Reclaiming Memory
    ... example, handles to device context, memory addresses, etc.), VB is actually ... VB will clean up references to objects all ... It's a little unusual to use an array to store object references. ... unusual to use a variant array for what you apparently are (at least I'VE ...
    (microsoft.public.vb.com)
  • Re: list implementation
    ... >I believe the type "list" is implemented as an array of pointers. ... A Python list is sematically/behaviorally defined as a mutable extensible ... the references are arrays of C pointers. ...
    (comp.lang.python)