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

📄 m.htm

📁 一个非常适合初学者入门的有关c++的文档
💻 HTM
📖 第 1 页 / 共 5 页
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Frameset//EN" "http://www.w3.org/TR/REC-html40/frameset.dtd">
<HTML LANG="EN">
<HEAD>
<TITLE>More Effective C++ | Book</TITLE>
<LINK REL=STYLESHEET HREF=../INTRO/ECMEC.CSS>

<SCRIPT LANGUAGE="Javascript" SRC="../JAVA/COOKIE.JS"></SCRIPT>
<SCRIPT LANGUAGE="Javascript">var imagemax = 35; setCurrentMax(35);</SCRIPT>
<SCRIPT LANGUAGE="Javascript" SRC="../JAVA/IMGDOC.JS"></SCRIPT>
<SCRIPT LANGUAGE="Javascript" SRC="../JAVA/NSIMGDOC.JS"></SCRIPT>
<SCRIPT LANGUAGE="Javascript" SRC="../JAVA/DINGBATS.JS"></SCRIPT>
<SCRIPT LANGUAGE="Javascript">var dingbase = "M_DIR.HTM"; var dingtext ="MEC++, P"; if (self == top) {
 top.location.replace(dingbase + this.location.hash);
}</SCRIPT>

</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000" ONLOAD="setResize()">
<A NAME="pxi"></A><A NAME="pxii"></A><A NAME="ded"></A>

<!-- SectionName="MEC++ Dedication" -->

<DIV ALIGN="CENTER"><FONT SIZE="-1">Continue to <A HREF="#pxi">Acknowledgments</A></FONT></DIV>

<P><A NAME="dingp1"></A><font ID="mgtitle">Dedication</font><SCRIPT>create_link(1);</SCRIPT>

</P>

<P><A NAME="dingp2"></A><I>For Clancy, my favorite enemy within.</I><SCRIPT>create_link(2);</SCRIPT>

</P>

<!-- SectionName="MEC++ Acknowledgments" -->
<A NAME="pxi"></A><A NAME="pxii"></A>

<DIV ALIGN="CENTER"><FONT SIZE="-1">Back to <A HREF="#ded">Dedication</A> <BR>Continue to <A HREF="#1708">Introduction</A></FONT></DIV>

<P><A NAME="dingp3"></A><font ID="mgtitle"> Acknowledgments</font><SCRIPT>create_link(3);</SCRIPT>

</P>

<P><A NAME="dingp4"></A><A NAME="723"> </A>
A great number of people helped bring this book into existence. Some contributed ideas for technical topics, some helped with the process of producing the book, and some just made life more fun while I was working on <NOBR>it.<SCRIPT>create_link(4);</SCRIPT>

</NOBR></P>

<P><A NAME="dingp5"></A><A NAME="737"> </A>
When the number of contributors to a book is large, it is not uncommon to dispense with individual acknowledgments in favor of a generic "Contributors to this book are too numerous to mention." I prefer to follow the expansive lead of John L. Hennessy and David A. Patterson in <NOBR><FONT COLOR="#FF0000" SIZE="-2"><B>&deg;</B></FONT><A HREF="http://www.awl.com/cseng/cgi-bin/cdquery.pl?name=comparch" onMouseOver="self.status='Computer Architecture: A Quantitative Approach'; return true" onMouseOut="self.status=self.defaultStatus" target="_top"><I>Computer</NOBR> Architecture: A Quantitative Approach</I></A> (Morgan Kaufmann, 1995). In addition to motivating the comprehensive acknowledgments that follow, their book provides hard data for the 90-10 rule, which I refer to in <a href="#40995"onMouseOver = "self.status = 'Item 16'; return true" onMouseOut = "self.status = self.defaultStatus">Item 16</A>.<SCRIPT>create_link(5);</SCRIPT>

</p>

<P><A NAME="dingp6"></A><font ID="mhtitle">The Items</font><SCRIPT>create_link(6);</SCRIPT>

</P>

<A NAME="2764"> </A>
<P><A NAME="dingp7"></A>
With the exception of direct quotations, all the words in this book are mine. However, many of the ideas I discuss came from others. I have done my best to keep track of who contributed what, but I know I have included information from sources I now fail to recall, foremost among them many posters to the Usenet newsgroups <NOBR><FONT COLOR="#FF0000" SIZE="-2"><B>&deg;</B></FONT><A HREF="http://www.awl.com/cseng/cgi-bin/cdquery.pl?name=clcfaq" ONMOUSEOVER ="self.status = 'comp.lang.c++ Newsgroup'; return true" ONMOUSEOUT="self.status=self.defaultStatus" TARGET="_top"><CODE>comp.lang.c++</CODE></NOBR></a> and <NOBR><FONT COLOR="#FF0000" SIZE="-2"><B>&deg;</B></FONT><A HREF="http://www.awl.com/cseng/cgi-bin/cdquery.pl?name=compstdc" ONMOUSEOVER ="self.status = 'comp.std.c++ Newsgroup'; return true" ONMOUSEOUT="self.status=self.defaultStatus" TARGET="_top">comp.std.c++</CODE></NOBR></a>.<SCRIPT>create_link(7);</SCRIPT>

</P><A NAME="1518"> </A>
<P><A NAME="dingp8"></A>
Many ideas in the C++ community have been developed independently by many people. In what follows, I note only where <I>I</I> was exposed to particular ideas, not necessarily where those ideas <NOBR>originated.<SCRIPT>create_link(8);</SCRIPT>

</NOBR></P><A NAME="2226"> </A>
<P><A NAME="dingp9"></A>
Brian Kernighan suggested the use of macros to approximate the syntax of the new C++ casting operators I describe in <A HREF="#77216">Item 2</A>.<SCRIPT>create_link(9);</SCRIPT>

</P><A NAME="480"> </A>
<P><A NAME="dingp10"></A>
In <A HREF="#84818">Item 3</A>, my warning about deleting an array of derived class objects through a base class pointer is based on material in Dan Saks' "Gotchas" talk, which he's given at several conferences and trade <NOBR>shows.<SCRIPT>create_link(10);</SCRIPT>

</NOBR></P><A NAME="794"> </A>
<P><A NAME="dingp11"></A>
<A NAME="pxii"></A>In <A HREF="#5970">Item 5</A>, the proxy class technique for preventing unwanted application of single-argument constructors is based on material in Andrew Koenig's column in the January 1994 <NOBR><FONT COLOR="#FF0000" SIZE="-2"><B>&deg;</B></FONT><A HREF="http://www.awl.com/cseng/cgi-bin/cdquery.pl?name=cppreport" onMouseOver="self.status='C++ Report Home Page'; return true" onMouseOut="self.status=self.defaultStatus" target="_top"><I>C++</NOBR> Report</I></A>.<SCRIPT>create_link(11);</SCRIPT>

</P><A NAME="3421"> </A>
<P><A NAME="dingp12"></A>
James Kanze made a posting to <NOBR><FONT COLOR="#FF0000" SIZE="-2"><B>&deg;</B></FONT><A HREF="http://www.awl.com/cseng/cgi-bin/cdquery.pl?name=clcfaq" ONMOUSEOVER ="self.status = 'comp.lang.c++ Newsgroup'; return true" ONMOUSEOUT="self.status=self.defaultStatus" TARGET="_top"><CODE>comp.lang.c++</CODE></NOBR></a> on implementing postfix increment and decrement operators via the corresponding prefix functions; I use his technique in <A HREF="#5262">Item 6</A>.<SCRIPT>create_link(12);</SCRIPT>

</P><A NAME="3426"> </A>
<P><A NAME="dingp13"></A>
David Cok, writing me about material I covered in <NOBR><A HREF="../EC/INDEX.HTM"><I>Effective</NOBR> C++</I></A>, brought to my attention the distinction between <CODE>operator</CODE> <CODE>new</CODE> and the <CODE>new</CODE> operator that is the crux of <A HREF="#33985">Item 8</A>. Even after reading his letter, I didn't really understand the distinction, but without his initial prodding, I probably <I>still</I> <NOBR>wouldn't.<SCRIPT>create_link(13);</SCRIPT>

</NOBR></P><A NAME="813"> </A>
<P><A NAME="dingp14"></A>
The notion of using destructors to prevent resource leaks (used in <A HREF="#5292">Item 9</A>) comes from section 15.3 of Margaret A. Ellis' and Bjarne Stroustrup's <NOBR><FONT COLOR="#FF0000" SIZE="-2"><B>&deg;</B></FONT><A HREF="http://www.awl.com/cseng/cgi-bin/cdquery.pl?name=anncrm" onMouseOver="self.status='Annotated C++ Reference Manual Home Page'; return true" onMouseOut="self.status=self.defaultStatus" target="_top"><I>The</NOBR> Annotated C++ Reference Manual</I></A> (see <a href="E.HTM#p285">page 285</a>). There the technique is called <I>resource acquisition is initialization</I>. Tom Cargill suggested I shift the focus of the approach from resource acquisition to resource <NOBR>release.<SCRIPT>create_link(14);</SCRIPT>

</NOBR></P><A NAME="1185"> </A>
<P><A NAME="dingp15"></A>
Some of my discussion in <a href="#39749"onMouseOver = "self.status = 'Item 11'; return true" onMouseOut = "self.status = self.defaultStatus">Item 11</A> was inspired by material in Chapter 4 of <NOBR><FONT COLOR="#FF0000" SIZE="-2"><B>&deg;</B></FONT><A HREF="http://www.awl.com/cseng/cgi-bin/cdquery.pl?name=talgdp" onMouseOver="self.status='Taligent`s Guide to Designing Programs Home Page'; return true" onMouseOut="self.status=self.defaultStatus" target="_top"><I>Taligent's</NOBR> Guide to Designing Programs</I></A> (Addison-Wesley, <NOBR>1994).<SCRIPT>create_link(15);</SCRIPT>

</NOBR></P><A NAME="451"> </A>
<P><A NAME="dingp16"></A>
My description of over-eager memory allocation for the <CODE>DynArray</CODE> class in <a href="#41124">Item 18</A> is based on Tom Cargill's article, "A Dynamic vector is harder than it looks," in the June 1992 <NOBR><FONT COLOR="#FF0000" SIZE="-2"><B>&deg;</B></FONT><A HREF="http://www.awl.com/cseng/cgi-bin/cdquery.pl?name=cppreport" onMouseOver="self.status='C++ Report Home Page'; return true" onMouseOut="self.status=self.defaultStatus" target="_top"><I>C++</NOBR> Report</I></A>. A more sophisticated design for a dynamic array class can be found in Cargill's follow-up column in the January 1994 <NOBR><FONT COLOR="#FF0000" SIZE="-2"><B>&deg;</B></FONT><A HREF="http://www.awl.com/cseng/cgi-bin/cdquery.pl?name=cppreport" onMouseOver="self.status='C++ Report Home Page'; return true" onMouseOut="self.status=self.defaultStatus" target="_top"><I>C++</NOBR> Report</I></A>.<SCRIPT>create_link(16);</SCRIPT>

</P><A NAME="441"> </A>
<P><A NAME="dingp17"></A>
<A HREF="#41187">Item 21</A> was inspired by Brian Kernighan's paper, "An AWK to C++ Translator," at the 1991 USENIX C++ Conference. His use of overloaded operators (sixty-seven of them!) to handle mixed-type arithmetic operations, though designed to solve a problem unrelated to the one I explore in <A HREF="#41187">Item 21</A>, led me to consider multiple overloadings as a solution to the problem of temporary <NOBR>creation.<SCRIPT>create_link(17);</SCRIPT>

</NOBR></P><A NAME="389"> </A>
<P><A NAME="dingp18"></A>
In <A HREF="#5350">Item 26</A>, my design of a template class for counting objects is based on a posting to <NOBR><FONT COLOR="#FF0000" SIZE="-2"><B>&deg;</B></FONT><A HREF="http://www.awl.com/cseng/cgi-bin/cdquery.pl?name=clcfaq" ONMOUSEOVER ="self.status = 'comp.lang.c++ Newsgroup'; return true" ONMOUSEOUT="self.status=self.defaultStatus" TARGET="_top"><CODE>comp.lang.c++</CODE></NOBR></a> by Jamshid <NOBR>Afshar.<SCRIPT>create_link(18);</SCRIPT>

</NOBR></P><A NAME="369"> </A>
<P><A NAME="dingp19"></A>
The idea of a mixin class to keep track of pointers from <CODE>operator</CODE> <CODE>new</CODE> (see <A HREF="#22627">Item 27</A>) is based on a suggestion by Don Box. Steve Clamage made the idea practical by explaining how <CODE>dynamic_cast</CODE> can be used to find the beginning of memory for an <NOBR>object.<SCRIPT>create_link(19);</SCRIPT>

</NOBR></P><A NAME="3572"> </A>
<P><A NAME="dingp20"></A>
The discussion of smart pointers in <A HREF="#61766">Item 28</A> is based in part on Steven Buroff's and Rob Murray's <I>C++ Oracle </I>column in the October 1993 <NOBR><FONT COLOR="#FF0000" SIZE="-2"><B>&deg;</B></FONT><A HREF="http://www.awl.com/cseng/cgi-bin/cdquery.pl?name=cppreport" onMouseOver="self.status='C++ Report Home Page'; return true" onMouseOut="self.status=self.defaultStatus" target="_top"><I>C++</NOBR> Report</I></A>; on Daniel R. Edelson's classic paper, "Smart Pointers: They're Smart, but They're Not Pointers," in the proceedings of the 1992 <A NAME="pxiii"></A>USENIX C++ Conference; on section 15.9.1 of Bjarne Stroustrup's <NOBR><FONT COLOR="#FF0000" SIZE="-2"><B>&deg;</B></FONT><A HREF="http://www.awl.com/cseng/cgi-bin/cdquery.pl?name=desec" onMouseOver="self.status='The Design and Evolution of C++ Home Page'; return true" onMouseOut="self.status=self.defaultStatus" target="_top"><I>The</NOBR> Design and Evolution of C++</I></A> (see <a href="E.HTM#p285">page 285</a>); on Gregory Colvin's "C++ Memory Management" class notes from C/C++ Solutions '95; and on Cay Horstmann's column in the March-April 1993 issue of the <NOBR><FONT COLOR="#FF0000" SIZE="-2"><B>&deg;</B></FONT><A HREF="http://www.awl.com/cseng/cgi-bin/cdquery.pl?name=cppreport" onMouseOver="self.status='C++ Report Home Page'; return true" onMouseOut="self.status=self.defaultStatus" target="_top"><I>C++</NOBR> Report</I></A>. I developed some of the material myself, though. <NOBR>Really.<SCRIPT>create_link(20);</SCRIPT>

</NOBR></P><A NAME="317"> </A>
<P><A NAME="dingp21"></A>
In <A HREF="#6073">Item 29</A>, the use of a base class to store reference counts and of smart pointers to manipulate those counts is based on Rob Murray's discussions of the same topics in sections 6.3.2 and 7.4.2, respectively, of his <NOBR><FONT COLOR="#FF0000" SIZE="-2"><B>&deg;</B></FONT><A HREF="http://www.awl.com/cseng/cgi-bin/cdquery.pl?name=cstrtac" onMouseOver="self.status='C++ Strategies and Tactics Home Page'; return true" onMouseOut="self.status=self.defaultStatus" target="_top"><I>C++</NOBR> Strategies and Tactics</I></A> (see page 286). The design for adding reference counting to existing classes follows that presented by Cay Horstmann in his March-April 1993 column in the <NOBR><FONT COLOR="#FF0000" SIZE="-2"><B>&deg;</B></FONT><A HREF="http://www.awl.com/cseng/cgi-bin/cdquery.pl?name=cppreport" onMouseOver="self.status='C++ Report Home Page'; return true" onMouseOut="self.status=self.defaultStatus" target="_top"><I>C++</NOBR> Report</I></A>.<SCRIPT>create_link(21);</SCRIPT>

</P><A NAME="411"> </A>
<P><A NAME="dingp22"></A>
In <A HREF="#6074">Item 30</A>, my discussion of lvalue contexts is based on comments in Dan Saks' column in the C User's Journal <NOBR><FONT COLOR="#FF0000" SIZE="-2"><B>&deg;</B></FONT><A HREF="http://www.awl.com/cseng/cgi-bin/cdquery.pl?name=cuj" onMouseOver="self.status='C/C++ Users Journal Home Page'; return true" onMouseOut="self.status=self.defaultStatus" target="_top">C/C++</NOBR> Users Journal</A>) of January 1993. The observation that non-proxy member functions are unavailable when called through proxies comes from an unpublished paper by Cay <NOBR>Horstmann.<SCRIPT>create_link(22);</SCRIPT>

</NOBR></P><A NAME="622"> </A>
<P><A NAME="dingp23"></A>
The use of runtime type information to build vtbl-like arrays of function pointers (in <a href="#34883">Item 31</A>) is based on ideas put forward by Bjarne Stroustrup in postings to <NOBR><FONT COLOR="#FF0000" SIZE="-2"><B>&deg;</B></FONT><A HREF="http://www.awl.com/cseng/cgi-bin/cdquery.pl?name=clcfaq" ONMOUSEOVER ="self.status = 'comp.lang.c++ Newsgroup'; return true" ONMOUSEOUT="self.status=self.defaultStatus" TARGET="_top"><CODE>comp.lang.c++</CODE></NOBR></a> and in section 13.8.1 of his <NOBR><FONT COLOR="#FF0000" SIZE="-2"><B>&deg;</B></FONT><A HREF="http://www.awl.com/cseng/cgi-bin/cdquery.pl?name=desec" onMouseOver="self.status='The Design and Evolution of C++ Home Page'; return true" onMouseOut="self.status=self.defaultStatus" target="_top">The</NOBR> Design and Evolution of C++</A> (see <a href="E.HTM#p285">page <NOBR>285</a>).<SCRIPT>create_link(23);</SCRIPT>

</NOBR></P><A NAME="171"> </A>
<P><A NAME="dingp24"></A>
The material in <A HREF="#10947">Item 33</A> is based on several of my <NOBR><FONT COLOR="#FF0000" SIZE="-2"><B>&deg;</B></FONT><A HREF="http://www.awl.com/cseng/cgi-bin/cdquery.pl?name=cppreport" onMouseOver="self.status='C++ Report Home Page'; return true" onMouseOut="self.status=self.defaultStatus" target="_top">C++</NOBR> Report</A> columns in 1994 and 1995. Those columns, in turn, included comments I received from Klaus Kreft about how to use <CODE>dynamic_cast</CODE> to implement a virtual <CODE>operator=</CODE> that detects arguments of the wrong <NOBR>type.<SCRIPT>create_link(24);</SCRIPT>

</NOBR></P><A NAME="600"> </A>
<P><A NAME="dingp25"></A>
Much of the material in <A HREF="#33950">Item 34</A> was motivated by Steve Clamage's article, "Linking C++ with other languages," in the May 1992 <NOBR><FONT COLOR="#FF0000" SIZE="-2"><B>&deg;</B></FONT><A HREF="http://www.awl.com/cseng/cgi-bin/cdquery.pl?name=cppreport" onMouseOver="self.status='C++ Report Home Page'; return true" onMouseOut="self.status=self.defaultStatus" target="_top">C++</NOBR> Report</A>. In that same Item, my treatment of the problems caused by functions like <CODE>strdup</CODE> was motivated by an anonymous <NOBR>reviewer.<SCRIPT>create_link(25);</SCRIPT>

</NOBR></p><P><A NAME="dingp26"></A><font ID="mhtitle">The Book</font><SCRIPT>create_link(26);</SCRIPT>
</P>
<A NAME="969"> </A>
<P><A NAME="dingp27"></A>
Reviewing draft copies of a book is hard &#151; and vitally important &#151; work. I am grateful that so many people were willing to invest their time and energy on my behalf. I am especially grateful to Jill Huchital, Tim Johnson, Brian Kernighan, Eric Nagler, and Chris Van Wyk, as they read the book (or large portions of it) more than once. In addition to these gluttons for punishment, complete drafts of the manuscript were read by Katrina Avery, Don Box, Steve Burkett, Tom Cargill, Tony Davis, Carolyn Duby, Bruce Eckel, Read Fleming, Cay Horstmann, James Kanze, Russ Paielli, Steve Rosenthal, Robin Rowe, Dan Saks, Chris Sells, Webb Stacy, Dave Swift, Steve Vinoski, and Fred Wild. Partial drafts were reviewed by Bob Beauchaine, Gerd Hoeren, <A NAME="pxiv"></A>Jeff Jackson, and Nancy L. Urbano. Each of these reviewers made comments that greatly improved the accuracy, utility, and presentation of the material you find <NOBR>here.<SCRIPT>create_link(27);</SCRIPT>

</NOBR></P><A NAME="2818"> </A>
<P><A NAME="dingp28"></A>
Once the book came out, I received corrections and suggestions from many people. I've listed these sharp-eyed readers in the order in which I received their missives: Luis Kida, John Potter, Tim Uttormark, Mike Fulkerson, Dan Saks, Wolfgang Glunz, Clovis Tondo, Michael Loftus, Liz Hanks, Wil Evers, Stefan Kuhlins, Jim McCracken, Alan Duchan, John Jacobsma, Ramesh Nagabushnam, Ed Willink, Kirk Swenson, Jack Reeves, Doug Schmidt, Tim Buchowski, Paul Chisholm, Andrew Klein, Eric Nagler, Jeffrey Smith, Sam Bent, Oleg Shteynbuk, Anton Doblmaier, Ulf Michaelis, Sekhar Muddana, Michael Baker, Yechiel Kimchi, David Papurt, Ian Haggard, Robert Schwartz, David Halpin, Graham Mark, David Barrett, Damian Kanarek, Ron Coutts, Lance Whitesel, Jon Lachelt, Cheryl Ferguson, Munir Mahmood, Klaus-Georg Adams, David Goh, Chris Morley, and Rainer Baumschlager. Their suggestions allowed me to improve <I>More Effective C++ </I>in updated printings (such as this one), and I greatly appreciate their <NOBR>help.<SCRIPT>create_link(28);</SCRIPT>

</NOBR></P><A NAME="2296"> </A>
<P><A NAME="dingp29"></A>
During preparation of this book, I faced many questions about the emerging <NOBR><FONT COLOR="#FF0000" SIZE="-2"><B>&deg;</B></FONT><A HREF="http://www.awl.com/cseng/cgi-bin/cdquery.pl?name=cstandard" onMouseOver="self.status='ISO/ANSI standard for C++'; return true" onMouseOut="self.status=self.defaultStatus" target="_top">ISO/ANSI</NOBR> standard for C++</A>, and I am grateful to Steve Clamage and Dan Saks for taking the time to respond to my incessant email <NOBR>queries.<SCRIPT>create_link(29);</SCRIPT>

⌨️ 快捷键说明

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