⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 wwwtc5.htm

📁 C++builder学习资料C++builder
💻 HTM
字号:


<HTML>

<HEAD>

   <TITLE> What's Wrong With This Code? Volume #5 Copy constructors</TITLE>

   <META NAME="Author" CONTENT="Harold Howe">

</HEAD>

<BODY>



<CENTER>

<TABLE  BORDER=0 CELLPADDING=0 CELLSPACING=0 WIDTH="640">

<TR>



<TD>







<H2>

What's Wrong With This Code? Volume #5

</H2>



<H4>

Copy constructors <BR>

<I> by Chris Uzdavinis</I>

</H4>



<P>

A programmer has written a base class and a derived class, and

instantiated an object of that derived class, called <TT>d1</TT>.  Later,

another Derived object is needed, a copy of <TT>d1</TT>. Here is the code.

</P>

<pre>

<font color="green">#include &lt;iostream&gt;</font>

<b>using</b> std<b>:</b><b>:</b>cout<b>;</b>

<b>using</b> std<b>:</b><b>:</b>endl<b>;</b>



<b>class</b> Base

<b>{</b>

<b>public</b><b>:</b>

  Base<b>(</b><b>)</b>                                    <b>{</b> cout <b>&lt;&lt;</b> <font color="blue">&quot;1&quot;</font> <b>&lt;&lt;</b> endl<b>;</b><b>}</b>

  Base<b>(</b>Base <b>const</b> <b>&amp;</b> rhs<b>)</b>                    <b>{</b> cout <b>&lt;&lt;</b> <font color="blue">&quot;2&quot;</font> <b>&lt;&lt;</b> endl<b>;</b><b>}</b>

  <b>virtual</b> <b>~</b>Base<b>(</b><b>)</b>                           <b>{</b> cout <b>&lt;&lt;</b> <font color="blue">&quot;3&quot;</font> <b>&lt;&lt;</b> endl<b>;</b><b>}</b>

<b>}</b><b>;</b>





<b>class</b> Derived <b>:</b> <b>public</b> Base

<b>{</b>

<b>public</b><b>:</b>

  Derived<b>(</b><b>)</b> <b>:</b> x_<b>(</b><font color="blue">0</font><b>)</b>                         <b>{</b> cout <b>&lt;&lt;</b> <font color="blue">&quot;4&quot;</font> <b>&lt;&lt;</b> endl<b>;</b><b>}</b>

  Derived<b>(</b><b>int</b> x<b>)</b> <b>:</b> x_<b>(</b>x<b>)</b>                    <b>{</b> cout <b>&lt;&lt;</b> <font color="blue">&quot;5&quot;</font> <b>&lt;&lt;</b> endl<b>;</b><b>}</b>

  Derived<b>(</b>Derived <b>const</b> <b>&amp;</b> rhs<b>)</b> <b>:</b> x_<b>(</b>rhs<b>.</b>x_<b>)</b> <b>{</b> cout <b>&lt;&lt;</b> <font color="blue">&quot;6&quot;</font> <b>&lt;&lt;</b> endl<b>;</b><b>}</b>

  <b>~</b>Derived<b>(</b><b>)</b> <b>{</b> <b>}</b>



  <b>int</b> get_x<b>(</b><b>)</b> <b>const</b> <b>{</b> <b>return</b> x_<b>;</b> <b>}</b>

<b>private</b><b>:</b>

  <b>int</b> x_<b>;</b>

<b>}</b><b>;</b>





main<b>(</b><b>)</b>

<b>{</b>

  Derived d1<b>(</b><font color="blue">999</font><b>)</b><b>;</b>

  <font color="navy">// copy construct d2 from d1</font>

  Derived d2<b>(</b>d1<b>)</b><b>;</b>

<b>}</b>

</pre>

<P>

What is the output of this program?  Is it surprising?  What needs to

be done to make it act like the programmer had probably expected?

</P>



<H4>

<A TARGET=_top href="wwwtc5answer.htm">Answer</A>

</H4>

<BR>

<TABLE  BORDER=1 CELLPADDING=10 CELLSPACING=0 WIDTH="100%">

<TR> <TD colspan = 2><B>Code for this edition</B> </TD> </TR>

<TR> <TD><TT><a HREF="download/wwwtc5.zip">wwwtc5.zip</a></TT></TD><TD>BCB4 project that contains problem code.</TD> </TR>

<TR> <TD><TT><a HREF="download/wwwtc5x.zip">wwwtc5x.zip</a></TT></TD><TD>Same project, includes an EXE (55 kb).</TD> </TR>

</TABLE>





</TD> </TR>



</TABLE>

</CENTER>

</BODY>

</HTML>

⌨️ 快捷键说明

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