page83.html

来自「wqeqwvrw rkjqhwrjwq jkhrjqwhrwq jkhrwq」· HTML 代码 · 共 72 行

HTML
72
字号
<HTML>
<HEAD>
<TITLE>Copy Constructor</TITLE>
</HEAD>
<BODY bgcolor="#FFFFFF">
 <img src="cover75.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/cover75.gif" alt="Logo" align=right>
<b>Data Structures and Algorithms 
with Object-Oriented Design Patterns in C++</b><br>
<A NAME="tex2html2929" HREF="page84.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page84.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="next_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/next_motif.gif"></A> <A NAME="tex2html2927" HREF="page80.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page80.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="up_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/up_motif.gif"></A> <A NAME="tex2html2921" HREF="page82.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page82.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="previous_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/previous_motif.gif"></A> <A NAME="tex2html2931" HREF="page9.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page9.html"><IMG WIDTH=65 HEIGHT=24 ALIGN=BOTTOM ALT="contents" SRC="contents_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/contents_motif.gif"></A> <A NAME="tex2html2932" HREF="page620.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page620.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="index_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/index_motif.gif"></A> <BR><HR>
<H2><A NAME="SECTION005130000000000000000">Copy Constructor</A></H2>
<A NAME="secfdsarraycc">&#160;</A>
The <em>copy constructor</em>
<A NAME=2702>&#160;</A><A NAME=2703>&#160;</A>
for an object of class <tt>T</tt> is the function <tt>T::T(T const&amp;)</tt>.
I.e., it is the constructor for objects of class <tt>T</tt>
which takes as its single argument
a reference to another object of class <tt>T</tt>.
This constructor builds a <em>copy</em> of the object passed by reference--hence the name <em>copy constructor</em>.
<P>
Copy constructors play a crucial r&#244;le in C++ programs.
For example, the copy constructor is called automatically by the compiler
to pass the value of an actual parameter used in a function call
to the formal parameter used in the function definition.
Similarly, the copy constructor is called automatically by the compiler
to pass the return value from a function back to the caller.
<P>
Program&nbsp;<A HREF="page83.html#progarray3c" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page83.html#progarray3c"><IMG  ALIGN=BOTTOM ALT="gif" SRC="cross_ref_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/cross_ref_motif.gif"></A> shows a simple,
though perhaps somewhat na&#305;ve implementation for the
copy constructor of <tt>Array&lt;T&gt;</tt> class objects.
To determine its running time,
we need to consider carefully the execution of this function.
<P>
<P><A NAME="2871">&#160;</A><A NAME="progarray3c">&#160;</A> <IMG WIDTH=575 HEIGHT=180 ALIGN=BOTTOM ALT="program2714" SRC="img596.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img596.gif"  ><BR>
<STRONG>Program:</STRONG> <tt>Array&lt;T&gt;</tt> Class Copy Constructor Definition<BR>
<P>
<P>
First, the required amount of memory is allocated by <tt>operator new</tt>.
As discussed above,
this involves finding space in the heap,
which we assume takes constant time;
and then initializing the elements of the array using
the default constructor for objects of type <tt>T</tt>,
which takes  <IMG WIDTH=122 HEIGHT=26 ALIGN=MIDDLE ALT="tex2html_wrap_inline60961" SRC="img597.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img597.gif"  >.
Next, the <tt>length</tt> field is set to the correct value,
which takes constant time.
<P>
Finally, the body of the <tt>Array&lt;T&gt;</tt> constructor
is a loop which copies one-by-one
the elements of the input array to the newly allocated array.
How this copy will actually be done depends on the type <tt>T</tt>.
We shall assume that the running time of this assignment
is the same as that of the copy constructor
for objects of type <tt>T</tt>.<A NAME="tex2html119" HREF="footnode.html#2873" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/footnode.html#2873"><IMG  ALIGN=BOTTOM ALT="gif" SRC="foot_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/foot_motif.gif"></A>
Then, the running time of the main loop of the <tt>Array&lt;T&gt;</tt> constructor
is  <IMG WIDTH=133 HEIGHT=26 ALIGN=MIDDLE ALT="tex2html_wrap_inline60963" SRC="img598.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img598.gif"  > where  <IMG WIDTH=72 HEIGHT=26 ALIGN=MIDDLE ALT="tex2html_wrap_inline60965" SRC="img599.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img599.gif"  > is the time taken by the copy constructor
for objects of type <tt>T</tt>.
<P>
Altogether, the running time of the copy constructor for <tt>Array&lt;T&gt;</tt> is
 <IMG WIDTH=278 HEIGHT=26 ALIGN=MIDDLE ALT="tex2html_wrap_inline60967" SRC="img600.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img600.gif"  >,
where <I>n</I> is the size of the array being copied.
In the case where <tt>T</tt> is one of the built-in types,
 <IMG WIDTH=89 HEIGHT=26 ALIGN=MIDDLE ALT="tex2html_wrap_inline60955" SRC="img595.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img595.gif"  > and  <IMG WIDTH=126 HEIGHT=26 ALIGN=MIDDLE ALT="tex2html_wrap_inline60973" SRC="img601.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img601.gif"  >,
which gives the simple and obvious result, <I>T</I>(<I>n</I>)=<I>O</I>(<I>n</I>).
<P>
<HR><A NAME="tex2html2929" HREF="page84.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page84.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="next_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/next_motif.gif"></A> <A NAME="tex2html2927" HREF="page80.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page80.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="up_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/up_motif.gif"></A> <A NAME="tex2html2921" HREF="page82.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page82.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="previous_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/previous_motif.gif"></A> <A NAME="tex2html2931" HREF="page9.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page9.html"><IMG WIDTH=65 HEIGHT=24 ALIGN=BOTTOM ALT="contents" SRC="contents_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/contents_motif.gif"></A> <A NAME="tex2html2932" HREF="page620.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page620.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="index_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/index_motif.gif"></A> <P><ADDRESS>
<img src="bruno.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/bruno.gif" alt="Bruno" align=right>
<a href="javascript:if(confirm('http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/copyright.html  \n\nThis file was not retrieved by Teleport Pro, because it is addressed on a domain or path outside the boundaries set for its Starting Address.  \n\nDo you want to open it from the server?'))window.location='http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/copyright.html'" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/copyright.html">Copyright &#169; 1997</a> by <a href="javascript:if(confirm('http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/signature.html  \n\nThis file was not retrieved by Teleport Pro, because it is addressed on a domain or path outside the boundaries set for its Starting Address.  \n\nDo you want to open it from the server?'))window.location='http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/signature.html'" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/signature.html">Bruno R. Preiss, P.Eng.</a>  All rights reserved.

</ADDRESS>
</BODY>
</HTML>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?