📄 ch13_04.htm
字号:
<html><head><title>The Copy Constructor (=) (Programming Perl)</title><!-- STYLESHEET --><link rel="stylesheet" type="text/css" href="../style/style1.css"><!-- METADATA --><!--Dublin Core Metadata--><meta name="DC.Creator" content=""><meta name="DC.Date" content=""><meta name="DC.Format" content="text/xml" scheme="MIME"><meta name="DC.Generator" content="XSLT stylesheet, xt by James Clark"><meta name="DC.Identifier" content=""><meta name="DC.Language" content="en-US"><meta name="DC.Publisher" content="O'Reilly & Associates, Inc."><meta name="DC.Source" content="" scheme="ISBN"><meta name="DC.Subject.Keyword" content=""><meta name="DC.Title" content="The Copy Constructor (=)"><meta name="DC.Type" content="Text.Monograph"></head><body><!-- START OF BODY --><!-- TOP BANNER --><img src="gifs/smbanner.gif" usemap="#banner-map" border="0" alt="Book Home"><map name="banner-map"><AREA SHAPE="RECT" COORDS="0,0,466,71" HREF="index.htm" ALT="Programming Perl"><AREA SHAPE="RECT" COORDS="467,0,514,18" HREF="jobjects/fsearch.htm" ALT="Search this book"></map><!-- TOP NAV BAR --><div class="navbar"><table width="515" border="0"><tr><td align="left" valign="top" width="172"><a href="ch13_03.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0"></a></td><td align="center" valign="top" width="171"><a href="ch13_01.htm">Chapter 13: Overloading</a></td><td align="right" valign="top" width="172"><a href="ch13_05.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0"></a></td></tr></table></div><hr width="515" align="left"><!-- SECTION BODY --><h2 class="sect1">13.4. The Copy Constructor (=)</h2><p><a name="INDEX-2628"></a><a name="INDEX-2629"></a><a name="INDEX-2630"></a>Although it looks like a regular operator, <tt class="literal">=</tt> has a special andslightly subintuitive meaning as an overload key. It does <em class="emphasis">not</em>overload the Perl assignment operator. It can't, because thatoperator has to be reserved for assigning references, or everythingbreaks.</p><p><a name="INDEX-2631"></a>The handler for <tt class="literal">=</tt> is used in situations where amutator (such as <tt class="literal">++</tt>, <tt class="literal">--</tt>, or any of the assignment operators)is applied to a reference that shares its object with anotherreference. The <tt class="literal">=</tt> handler lets you intercept the mutator andcopy the object yourself so that the copy alone is mutated.Otherwise, you'd clobber the original.<blockquote><pre class="programlisting">$copy = $original; # copies only the reference++$copy; # changes underlying shared object</pre></blockquote>Now, bear with us. Suppose that <tt class="literal">$original</tt> is areference to an object. To make <tt class="literal">++$copy</tt> modifyonly <tt class="literal">$copy</tt> and not <tt class="literal">$original</tt>, acopy of <tt class="literal">$copy</tt> is first made, and<tt class="literal">$copy</tt> is assigned a reference to this new object.This operation is not performed until <tt class="literal">++$copy</tt> isexecuted, so <tt class="literal">$copy</tt> coincides with<tt class="literal">$original</tt> before the increment--but notafterward. In other words, it's the <tt class="literal">++</tt> thatrecognizes the need for the copy and calls out to your copyconstructor.</p><p>The need for copying is recognized only by mutators such as<tt class="literal">++</tt> or <tt class="literal">+=</tt>, or by <tt class="literal">nomethod</tt>, which is describedlater. If the operation is autogenerated via <tt class="literal">+</tt>, as in:<blockquote><pre class="programlisting">$copy = $original;$copy = $copy + 1;</pre></blockquote>then no copying occurs, because <tt class="literal">+</tt> doesn't know it's being used as a mutator.</p><p>If the copy constructor is required during the execution of some mutator,but a handler for <tt class="literal">=</tt> was not specified, it can be autogenerated as astring copy provided the object is a plain scalar and not something fancier.</p><p>For example, the code actually executed for the sequence:<blockquote><pre class="programlisting">$copy = $original;...++$copy;</pre></blockquote>might end up as something like this:<blockquote><pre class="programlisting">$copy = $original;...$copy = $copy->clone(undef, "");$copy->incr(undef, "");</pre></blockquote>This assumes <tt class="literal">$original</tt> points to an overloadedobject, <tt class="literal">++</tt> was overloaded with<tt class="literal">\&incr</tt>, and <tt class="literal">=</tt> was overloadedwith <tt class="literal">\&clone</tt>.</p><p>Similar behavior is triggered by <tt class="literal">$copy =$original++</tt>, which is interpreted as <tt class="literal">$copy =$original; ++$original</tt>.</p><!-- BOTTOM NAV BAR --><hr width="515" align="left"><div class="navbar"><table width="515" border="0"><tr><td align="left" valign="top" width="172"><a href="ch13_03.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0"></a></td><td align="center" valign="top" width="171"><a href="index.htm"><img src="../gifs/txthome.gif" alt="Home" border="0"></a></td><td align="right" valign="top" width="172"><a href="ch13_05.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0"></a></td></tr><tr><td align="left" valign="top" width="172">13.3. Overloadable Operators</td><td align="center" valign="top" width="171"><a href="index/index.htm"><img src="../gifs/index.gif" alt="Book Index" border="0"></a></td><td align="right" valign="top" width="172">13.5. When an Overload Handler Is Missing (nomethod and fallback)</td></tr></table></div><hr width="515" align="left"><!-- LIBRARY NAV BAR --><img src="../gifs/smnavbar.gif" usemap="#library-map" border="0" alt="Library Navigation Links"><p><font size="-1"><a href="copyrght.htm">Copyright © 2001</a> O'Reilly & Associates. All rights reserved.</font></p><map name="library-map"> <area shape="rect" coords="2,-1,79,99" href="../index.htm"><area shape="rect" coords="84,1,157,108" href="../perlnut/index.htm"><area shape="rect" coords="162,2,248,125" href="../prog/index.htm"><area shape="rect" coords="253,2,326,130" href="../advprog/index.htm"><area shape="rect" coords="332,1,407,112" href="../cookbook/index.htm"><area shape="rect" coords="414,2,523,103" href="../sysadmin/index.htm"></map><!-- END OF BODY --></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -