Re: Copying IDL objects [message #13057 is a reply to message #12981] |
Fri, 02 October 1998 00:00  |
davidf
Messages: 2866 Registered: September 1996
|
Senior Member |
|
|
Mark Rivers (rivers@cars3.uchicago.edu) writes:
> I am trying to figure out how to make a copy of an instance of an IDL object.
>
> a = obj_new('myobj')
> b = a
> ; This does not do what I want, it makes a new reference to the same instance
>
> b = obj_copy(a) ; This is what I want
I thought Replicate might do it, but I see it also just
replicates the object reference, not the actual object.
> The only way I can see how to do it is:
>
> pro my_obj::copy, out
> out->set_a, self.a
> out->set_b, self.b
> ....
> end
>
> This means I have to write a set_X function for each field, and each object
> class needs its own version of the ::copy routine. This seems crazy.
This seems like a lot of work. I think I would write this
function something like this:
PRO My_Obj::Copy, out
tags = N_Tags(self)
For j=0,tags-1 DO out.(j} = self.(j)
END
The only drawback is that N_Tags (and Tag_Names, too) will
only find the tags or fields in the outermost structure.
I do have a little recursive function on my web page to
find *all* the tags in an embedded structure definition.
http://www.dfanning.com/tips/recursive_struct_tags.html
Cheers,
David
----------------------------------------------------------
David Fanning, Ph.D.
Fanning Software Consulting
E-Mail: davidf@dfanning.com
Phone: 970-221-0438, Toll-Free Book Orders: 1-888-461-0155
Coyote's Guide to IDL Programming: http://www.dfanning.com/
|
|
|