wwwtc1.htm
来自「C++builder学习资料C++builder」· HTM 代码 · 共 83 行
HTM
83 行
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD><TITLE>What's Wrong With This Code? Volume #1</TITLE>
<META content="text/html; charset=gb2312" http-equiv=Content-Type>
<META content="Harold Howe" name=Author>
<META content="MSHTML 5.00.2614.3500" name=GENERATOR></HEAD>
<BODY>
<CENTER>
<TABLE border=0 cellPadding=0 cellSpacing=0 width=640>
<TBODY>
<TR>
<TD>
<H2>What's Wrong With This Code? Volume #1 </H2>
<H4>The assignment operator </H4>
<P>Experienced programmers will tell you that most C++ classes should
contain, at a minimum, a default constructor, a copy constructor, an
assignment operator, and a destructor. The following code example contains
a class called <TT>Foo</TT> that attempts to follow these guidelines. The
code also contains a factory method called <TT>GetAFoo</TT>, whose sole
purpose is to create and return new <TT>Foo</TT> objects. </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>"default constructor"</FONT> <B><<</B> endl<B>;</B>
<B>}</B>
Foo<B>(</B><B>const</B> Foo <B>&</B><B>)</B>
<B>{</B>
cout <B><<</B> <FONT color=blue>"copy construction"</FONT> <B><<</B> endl<B>;</B>
<B>}</B>
<B>~</B>Foo<B>(</B><B>)</B>
<B>{</B>
cout <B><<</B> <FONT color=blue>"destructor"</FONT> <B><<</B> endl<B>;</B>
<B>}</B>
Foo<B>&</B> <B>operator</B><B>=</B><B>(</B>Foo <B>&</B>rhs<B>)</B>
<B>{</B>
cout <B><<</B> <FONT color=blue>"assignment"</FONT> <B><<</B> endl<B>;</B>
<B>return</B> <B>*</B><B>this</B><B>;</B>
<B>}</B>
<B>}</B><B>;</B>
Foo GetAFoo<B>(</B><B>)</B>
<B>{</B>
Foo foo<B>;</B>
<B>return</B> foo<B>;</B>
<B>}</B>
<B>int</B> main<B>(</B><B>)</B>
<B>{</B>
Foo foo1<B>;</B>
Foo foo2<B>;</B>
foo1 <B>=</B> GetAFoo<B>(</B><B>)</B><B>;</B> <FONT color=navy>// generates compiler error</FONT>
foo2 <B>=</B> foo1<B>;</B> <FONT color=navy>// compiles OK</FONT>
<B>return</B> <FONT color=blue>0</FONT><B>;</B>
<B>}</B>
</PRE>
<P>Notice that the <TT>Foo</TT> class provides a default constructor, a
copy constructor, a destructor, and an assignment operator. Despite our
best intentions, the code contains a couple of problems. First and
foremost, it does not compile. The compiler error is: </P><PRE>[C++ Error] broken.cpp(43):
E2285 Could not find a match for 'Foo::operator =(Foo)'.
</PRE>
<P>The <TT>Foo</TT> class clearly provides an assignment operator that
allows you to assign one <TT>Foo</TT> object to another. But the compiler
complains that it can't find an assignment operator for the assignment to
<TT>foo1</TT>. This seems strange, since the assignment to <TT>foo2</TT>
compiles fine. So what's the difference between the two? Can you find the
problem and fix the code so it compiles without error? </P>
<P>In addition to fixing the compilation error, see if you can streamline
the code a little bit. There are at least two places where you could
improve the runtime performance of the program by making only minor
changes to the source. </P>
<H4><A href="wwwtc1answer.htm"
target=_top>Answer</A> </H4></TD></TR></TBODY></TABLE></CENTER></BODY></HTML>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?