📄 wwwtc4.htm
字号:
<HTML>
<HEAD>
<TITLE> What's Wrong With This Code? Volume #4</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 #4
</H2>
<H4>
Base class destructors
</H4>
<P>
The code example below contains three classes: <TT>Foo</TT>, <TT>Base</TT>, and
<TT>Derived</TT>. The <TT>Derived</TT> class contains two data members that are <TT>Foo</TT>
objects. One is a pointer, and one is not. <TT>Derived</TT> publically inherits from
<TT>Base</TT>.
</P>
<pre>
<font color="green">#include <iostream></font>
<b>using</b> <b>namespace</b> std<b>;</b>
<b>class</b> Foo
<b>{</b>
<b>public</b><b>:</b>
Foo<b>(</b><b>)</b>
<b>{</b>
cout <b><<</b> <font color="blue">"Foo constructor"</font> <b><<</b> endl<b>;</b>
<b>}</b>
<b>~</b>Foo<b>(</b><b>)</b>
<b>{</b>
cout <b><<</b> <font color="blue">"Foo destructor"</font> <b><<</b> endl<b>;</b>
<b>}</b>
<b>}</b><b>;</b>
<b>class</b> Base
<b>{</b>
<b>public</b><b>:</b>
Base<b>(</b><b>)</b>
<b>{</b>
cout <b><<</b> <font color="blue">"base constructor"</font> <b><<</b> endl<b>;</b>
<b>}</b>
<b>~</b>Base<b>(</b><b>)</b>
<b>{</b>
cout <b><<</b> <font color="blue">"base destructor"</font> <b><<</b> endl<b>;</b>
<b>}</b>
<b>}</b><b>;</b>
<b>class</b> Derived <b>:</b> <b>public</b> Base
<b>{</b>
Foo foo1<b>;</b>
Foo <b>*</b>foo2<b>;</b>
<b>public</b><b>:</b>
Derived<b>(</b><b>)</b>
<b>:</b>Base<b>(</b><b>)</b>
<b>{</b>
cout <b><<</b> <font color="blue">"derived constructor"</font> <b><<</b> endl<b>;</b>
foo2 <b>=</b> <b>new</b> Foo<b>;</b>
<b>}</b>
<b>~</b>Derived<b>(</b><b>)</b>
<b>{</b>
cout <b><<</b> <font color="blue">"derived destructor"</font> <b><<</b> endl<b>;</b>
<b>delete</b> foo2<b>;</b>
<b>}</b>
<b>}</b><b>;</b>
<font color="navy">//---------------------------------------------------------------------------</font>
<b>int</b> main<b>(</b><b>)</b>
<b>{</b>
Base <b>*</b>b <b>=</b> <b>new</b> Derived<b>;</b>
<b>delete</b> b<b>;</b>
<b>return</b> <font color="blue">0</font><b>;</b>
<b>}</b>
</pre>
<P>
The output from this program is:
</P>
<pre>
base constructor
Foo constructor
derived constructor
Foo constructor
base destructor
</pre>
<P>
The program seems to run OK. It does not crash and burn, and it does not hang the operating
system. On Windows, that's always a major accomplishment. However, the output from the program indicates that
something is wrong. Can you find the problem?
</P>
<H4>
<A TARGET=_top href="wwwtc4answer.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/wwwtc4.zip">wwwtc4.zip</a></TT></TD><TD>BCB4 project that contains problem code.</TD> </TR>
<TR> <TD><TT><a HREF="download/wwwtc4x.zip">wwwtc4x.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 + -