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

📄 m.htm

📁 教你如何编写有效简洁的出C++程序
💻 HTM
📖 第 1 页 / 共 5 页
字号:
</NOBR></P><A NAME="1947"> </A>
<P><A NAME="dingp30"></A>
John Max Skaller and Steve Rumsby conspired to get me the HTML for the draft ANSI C++ standard before it was widely available. Vivian Neou pointed me to the <NOBR><FONT COLOR="#FF0000" SIZE="-2"><B>&deg;</B></FONT><A HREF="http://www.awl.com/cseng/cgi-bin/cdquery.pl?name=ns" onMouseOver="self.status='Netscape Communicator Home Page'; return true" onMouseOut="self.status=self.defaultStatus" target="_top">Netscape</NOBR> WWW browser</A> as a stand-alone HTML viewer under (16 bit) Microsoft Windows, and I am deeply grateful to the folks at Netscape Communications for making their fine viewer freely available on such a pathetic excuse for an operating <NOBR>system.<SCRIPT>create_link(30);</SCRIPT>

</NOBR></P><A NAME="1165"> </A>
<P><A NAME="dingp31"></A>
Bryan Hobbs and Hachemi Zenad generously arranged to get me a copy of the internal engineering version of the <NOBR><FONT COLOR="#FF0000" SIZE="-2"><B>&deg;</B></FONT><A HREF="http://www.awl.com/cseng/cgi-bin/cdquery.pl?name=mw" onMouseOver="self.status='Metaware Home Page'; return true" onMouseOut="self.status=self.defaultStatus" target="_top">MetaWare</NOBR> C++ compiler</A> so I could check the code in this book using the latest features of the language. Cay Horstmann helped me get the compiler up and running in the very foreign world of DOS and DOS extenders. Borland (now <NOBR><FONT COLOR="#FF0000" SIZE="-2"><B>&deg;</B></FONT><A HREF="http://www.awl.com/cseng/cgi-bin/cdquery.pl?name=inp" onMouseOver="self.status='Inprise Home Page'; return true" onMouseOut="self.status=self.defaultStatus" target="_top">Inprise</A>)</NOBR> provided a beta copy of their most advanced compiler, and Eric Nagler and Chris Sells provided invaluable help in testing code for me on compilers to which I had no <NOBR>access.<SCRIPT>create_link(31);</SCRIPT>

</NOBR></P><A NAME="2009"> </A>
<P><A NAME="dingp32"></A>
Without the staff at the Corporate and Professional Publishing Division of Addison-Wesley, there would be no book, and I am indebted to Kim Dawley, Lana Langlois, Simone Payment, Marty Rabinowitz, Pradeepa Siva, John Wait, and the rest of the staff for their encouragement, patience, and help with the production of this <NOBR>work.<SCRIPT>create_link(32);</SCRIPT>

</NOBR></P><A NAME="2969"> </A>
<P><A NAME="dingp33"></A>
<A NAME="pxv"></A>Chris Guzikowski helped draft the back cover copy for this book, and Tim Johnson stole time from his research on low-temperature physics to critique later versions of that <NOBR>text.<SCRIPT>create_link(33);</SCRIPT>

</NOBR></P><A NAME="2970"> </A>
<P><A NAME="dingp34"></A>
Tom Cargill graciously agreed to make his <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++ Report</I></A> <A HREF="../MAGAZINE/CA_FRAME.HTM" TARGET="_top">article</NOBR> on exceptions</A> available.<SCRIPT>create_link(34);</SCRIPT>

</NOBR></p>
<P><A NAME="dingp35"></A><font ID="mhtitle">The People</font><SCRIPT>create_link(35);</SCRIPT>

</P>

<A NAME="2813"> </A>
<P><A NAME="dingp36"></A>
Kathy Reed was responsible for my introduction to programming; surely she didn't deserve to have to put up with a kid like me. Donald French had faith in my ability to develop and present C++ teaching materials when I had no track record. He also introduced me to John Wait, my editor at Addison-Wesley, an act for which I will always be grateful. The triumvirate at Beaver Ridge &#151; Jayni Besaw, Lorri Fields, and Beth McKee &#151; provided untold entertainment on my breaks as I worked on the <NOBR>book.<SCRIPT>create_link(36);</SCRIPT>

</NOBR></P><A NAME="1760"> </A>
<P><A NAME="dingp37"></A>
My wife, Nancy L. Urbano, put up with me and put up with me and put up with me as I worked on the book, continued to work on the book, and kept working on the book. How many times did she hear me say we'd do something after the book was done? Now the book is done, and we will do those things. She amazes me. I love <NOBR>her.<SCRIPT>create_link(37);</SCRIPT>

</NOBR></P><A NAME="671"> </A>
<P><A NAME="dingp38"></A>
Finally, I must acknowledge our puppy, <NOBR><FONT COLOR="#FF0000" SIZE="-2"><B>&deg;</B></FONT><A HREF="http://www.awl.com/cseng/cgi-bin/cdquery.pl?name=pers" onMouseOver="self.status='Persephones Home Page'; return true" onMouseOut="self.status=self.defaultStatus" target="_top">Persephone</A>,</NOBR> whose existence changed our world forever. Without her, this book would have been finished both sooner and with less sleep deprivation, but also with substantially less comic <NOBR>relief.<SCRIPT>create_link(38);</SCRIPT>

</NOBR></p>

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

<A NAME="p1"></A><A NAME="1708"> </A>

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

<P><A NAME="dingp39"></A><font ID="mgtitle">Introduction</font><SCRIPT>create_link(39);</SCRIPT>

</P>

<p><A NAME="dingp40"></A><A NAME="73953"> </A>
These are heady days for C++ programmers. Commercially available less than a decade, C++ has nevertheless emerged as the language of choice for systems programming on nearly all major computing platforms. Companies and individuals with challenging programming problems increasingly embrace the language, and the question faced by those who do not use C++ is often <I>when</I> they will start, not <I>if</I>. Standardization of C++ is complete, and the breadth and scope of the accompanying <a href="../EC/E_FR.HTM#8392" target="_top">library</a> &#151; which both dwarfs and subsumes that of C &#151; makes it possible to write rich, complex programs without sacrificing portability or implementing common algorithms and data structures from scratch. C++ compilers continue to proliferate, the features they offer continue to expand, and the quality of the code they generate continues to improve. Tools and environments for C++ development grow ever more abundant, powerful, and robust. Commercial libraries all but obviate the need to write code in many application <NOBR>areas.<SCRIPT>create_link(40);</SCRIPT>

</NOBR></P><A NAME="71383"> </A>

<P><A NAME="dingp41"></A>
As the language has matured and our experience with it has increased, our needs for information about it have changed. In 1990, people wanted to know <I>what</I> C++ was. By 1992, they wanted to know <I>how</I> to make it work. Now C++ programmers ask higher-level questions: How can I design my software so it will adapt to future demands? How can I improve the efficiency of my code without compromising its correctness or making it harder to use? How can I implement sophisticated functionality not directly supported by the <NOBR>language?<SCRIPT>create_link(41);</SCRIPT>

</NOBR></P><A NAME="72559"> </A>

<P><A NAME="dingp42"></A>
In this book, I answer these questions and many others like <NOBR>them.<SCRIPT>create_link(42);</SCRIPT>

</NOBR></P><A NAME="71453"> </A>

<P><A NAME="dingp43"></A>
This book shows how to design and implement C++ software that is <I>more effective</I>: more likely to behave correctly; more robust in the face of exceptions; more efficient; more portable; makes better use of language features; adapts to change more gracefully; works better in a mixed-language environment; is easier to use correctly; is harder to use incorrectly. In short, software that's just <I>better</I>.<SCRIPT>create_link(43);</SCRIPT>

</P><A NAME="74649"> </A>

<P><A NAME="dingp44"></A>
<A NAME="p2"></A>The material in this book is divided into 35 Items. Each Item summarizes accumulated wisdom of the C++ programming community on a particular topic. Most Items take the form of guidelines, and the explanation accompanying each guideline describes why the guideline exists, what happens if you fail to follow it, and under what conditions it may make sense to violate the guideline <NOBR>anyway.<SCRIPT>create_link(44);</SCRIPT>

</NOBR></P><A NAME="72734"> </A>

<P><A NAME="dingp45"></A>
Items fall into several categories. Some concern particular language features, especially newer features with which you may have little experience. For example, Items <A HREF="#5292">9</A> through <A HREF="#40989">15</A> are devoted to exceptions (as are the <A HREF="../MAGAZINE/INDEX.HTM" TARGET="_top">magazine articles</A> by Tom Cargill, Jack Reeves, and Herb Sutter). Other Items explain how to combine the features of the language to achieve higher-level goals. Items <a href="#5341">25</A> through <a href="#34883">31</A>, for instance, describe how to constrain the number or placement of objects, how to create functions that act "virtual" on the type of more than one object, how to create "smart pointers," and more. Still other Items address broader topics; Items <A HREF="#40995">16</A> through <a href="#41284">24</A> focus on efficiency. No matter what the topic of a particular Item, each takes a no-nonsense approach to the subject. In <a href="./INDEX.HTM" target="_top"><I>More Effective C++</I></A>, you learn how to <I>use</I> C++ more effectively. The descriptions of language features that make up the bulk of most C++ texts are in this book mere background <NOBR>information.<SCRIPT>create_link(45);</SCRIPT>

</NOBR></P><A NAME="48627"> </A>

<P><A NAME="dingp46"></A>
An implication of this approach is that you should be familiar with C++ before reading this book. I take for granted that you understand classes, protection levels, virtual and nonvirtual functions, etc., and I assume you are acquainted with the concepts behind templates and exceptions. At the same time, I don't expect you to be a language expert, so when poking into lesser-known corners of C++, I always explain what's going <NOBR>on.<SCRIPT>create_link(46);</SCRIPT>

</NOBR></p>

<P><A NAME="dingp47"></A><font ID="mhtitle">The C++ in <i>More Effective C++</i></font><SCRIPT>create_link(47);</SCRIPT>

</P>

<A NAME="73855"> </A>
<P><A NAME="dingp48"></A>
The C++ I describe in this book is the language specified by the <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='C++ Language Standard Home Page'; return true"
onMouseOut="self.status=self.defaultStatus" target="_top">Final</NOBR> Draft
International Standard</A> of the <NOBR><FONT COLOR="#FF0000"
SIZE="-2"><B>&deg;</B></FONT><A
HREF="http://www.awl.com/cseng/cgi-bin/cdquery.pl?name=committee"
onMouseOver="self.status='ISO/ANSI Standardization Committee Home Page';
return true" onMouseOut="self.status=self.defaultStatus"
target="_top">ISO/ANSI</NOBR> standardization committee</A> in November 1997.
In all likelihood, this means I use a few features your compilers don't yet
support. Don't worry. The only "new" feature I assume you have is templates,
and templates are now almost universally available. I use exceptions, too,
but that use is largely confined to Items <A HREF="#5292">9</A> through <A
HREF="#40989">15</A>, which are specifically devoted to exceptions. If you
don't have access to a compiler offering exceptions, that's okay. It won't
affect your ability to take advantage of the material in the other parts of
the book. Furthermore, you should read Items <A HREF="#5292">9</A> through <A
HREF="#40989">15</A> even if you don't have support for exceptions, because
those items (as well as the <A HREF="../MAGAZINE/INDEX.HTM"
TARGET="_top">associated articles</A>) examine issues you need to understand
in any <NOBR>case.<SCRIPT>create_link(48);</SCRIPT>

</NOBR></P><A NAME="73315"> </A>

<P><A NAME="dingp49"></A>
I recognize that just because the standardization committee blesses a feature or endorses a practice, there's no guarantee that the feature is present in current compilers or the practice is applicable to existing <A NAME="p3"></A>environments. When faced with a discrepancy between theory (what the committee says) and practice (what actually works), I discuss both, though my bias is toward things that work. Because I discuss both, this book will aid you as your compilers approach conformance with the standard. It will show you how to use existing constructs to approximate language features your compilers don't yet support, and it will guide you when you decide to transform workarounds into newly- supported <NOBR>features.<SCRIPT>create_link(49);</SCRIPT>

</NOBR></P><A NAME="48772"> </A>

<P><A NAME="dingp50"></A>
Notice that I refer to your <I>compilers </I>&#151; plural. Different compilers implement varying approximations to the standard, so I encourage you to develop your code under at least two compilers. Doing so will help you avoid inadvertent dependence on one vendor's proprietary language extension or its misinterpretation of the standard. It will also help keep you away from the bleeding edge of compiler technology, e.g., from new features supported by only one vendor. Such features are often poorly implemented (buggy or slow &#151; frequently both), and upon their introduction, the C++ community lacks experience to advise you in their proper use. Blazing trails can be exciting, but when your goal is producing reliable code, it's often best to let others test the waters before jumping <NOBR>in.<SCRIPT>create_link(50);</SCRIPT>

</NOBR></P><A NAME="71704"> </A>

<P><A NAME="dingp51"></A>
There are two constructs you'll see in this book that may not be familiar to you. Both are relatively recent language extensions. Some compilers support them, but if your compilers don't, you can easily approximate them with features you do <NOBR>have.<SCRIPT>create_link(51);</SCRIPT>

</NOBR></P><A NAME="71736"> </A>
<A NAME="73963"></A>
<P><A NAME="dingp52"></A>
The first construct is the <CODE>bool</CODE> type, which has as its values the keywords <CODE>true</CODE> and <CODE>false</CODE>. If your compilers haven't implemented <CODE>bool</CODE>, there are two ways to approximate it. One is to use a global <NOBR>enum:<SCRIPT>create_link(52);</SCRIPT>

</NOBR></P>

<UL><A NAME="71715"> </A>
<PRE>enum bool { false, true };
</PRE>
</UL>

<A NAME="71748"> </A>
<P><A NAME="dingp53"></A>
This allows you to overload functions on the basis of whether they take a <CODE>bool</CODE> or an <CODE>int</CODE>, but it has the disadvantage that the built-in comparison operators (i.e., <CODE>==</CODE>, <CODE>&lt;</CODE>, <CODE>&gt;=</CODE>, etc.) still return <CODE>int</CODE>s. As a result, code like the following will not behave the way it's supposed <NOBR>to:<SCRIPT>create_link(53);</SCRIPT>

⌨️ 快捷键说明

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