<h1>oo::copy<h1>
<H3><A
<b>oo::copy</b> &mdash; create copies of objects and classes
<H3><A
package require TclOO<BR>
<BR>
<B>oo::copy</B><I> sourceObject
<H3><A
The <B>oo::copy</B> command creates a copy of an object or class. It takes the
name of the object or class to be copied, <I>sourceObject</I>, and optionally
the name of the object or class to create, <I>targetObject</I>, which will be
resolved relative to the current namespace if not an absolute qualified name.
If <I>targetObject</I> is omitted, a new name is chosen. The copied object will
be of the same class as the source object, and will have all its per-object
methods copied. If it is a class, it will also have all the class methods in
the class copied, but it will not have any of its instances copied.
<P>
After the <I>targetObject</I> has been created and all definitions of its
configuration (e.g., methods, filters, mixins) copied, the <B>&lt;cloned&gt;</B>
method of <I>targetObject</I> will be invoked, to allow for customization of
the created object such as installing related variable traces. The only
argument given will be <I>sourceObject</I>. The default implementation of this
method (in <B><A just copies the procedures and variables in the
namespace of <I>sourceObject</I> to the namespace of <I>targetObject</I>. If
this method call does not return a result that is successful (i.e., an error
or other kind of exception) then the <I>targetObject</I> will be deleted and an
error returned.
<P>
The result of the <B>oo::copy</B> command will be the fully-qualified name of
the new object or class.
<H3><A
This example creates an object, copies it, modifies the source object, and
then demonstrates that the copied object is indeed a copy.
<P>
<PRE>oo::object create src
oo::objdefine src method msg {} {puts foo}
<B>oo::copy</B> src dst
oo::objdefine src method msg {} {puts bar}
src msg              <I><font size="+1">&#8594;</font> prints &quot;bar&quot;</I>
dst msg              <I><font size="+1">&#8594;</font> prints
<H3><A NAME="M6">SEE
<B><A <B><A <B><A
<H3><A
clone, copy, duplication, object