📄 eiintro.htm
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Frameset//EN" "http://www.w3.org/TR/REC-html40/frameset.dtd">
<HTML LANG="EN">
<HEAD>
<title>Effective C++, 2E | Introduction</TITLE>
<LINK REL=STYLESHEET HREF=../INTRO/ECMEC.CSS>
<SCRIPT LANGUAGE="Javascript" SRC="../JAVA/COOKIE.JS"></SCRIPT>
<SCRIPT LANGUAGE="Javascript">var imagemax = 0; setCurrentMax(0);</SCRIPT>
<SCRIPT LANGUAGE="Javascript" SRC="../JAVA/DINGBATS.JS"></SCRIPT>
<SCRIPT>
var dingbase = "EIINTDIR.HTM";
var dingtext = "EC++ Intro, P";
if (self == top) {
top.location.replace(dingbase + this.location.hash);
}
</SCRIPT>
</HEAD>
<BODY BGCOLOR="#FFFFFF" ONLOAD="setResize()">
<!-- SectionName="EC++ Introduction" -->
<DIV ALIGN="CENTER"><FONT SIZE="-1">Back to <A HREF="./EIACK_FR.HTM" TARGET="_top">Acknowledgments</A> <BR> Continue to <A HREF="./ESHIFTFR.HTM" TARGET="_top">Shifting from C to C++</A></FONT></DIV>
<P><A NAME="dingp1"></A><A NAME="p1"></A><FONT ID="egtitle">Introduction</FONT><SCRIPT>create_link(1);</SCRIPT>
</P>
<P><A NAME="dingp2"></A><A NAME="1709"></A>
Learning the fundamentals of a programming language is one thing; learning how to design and implement <I>effective</I> programs in that language is something else entirely. This is especially true of C++, a language boasting an uncommon range of power and expressiveness. Built atop a full-featured conventional language (C), it also offers a wide range of object-oriented features, as well as support for templates and <NOBR>exceptions.<SCRIPT>create_link(2);</SCRIPT>
</NOBR></P>
<p><A NAME="dingp3"></A><A NAME="1710"></A>
Properly used, C++ can be a joy to work with. An enormous variety of designs, both object-oriented and conventional, can be expressed directly and implemented efficiently. You can define new data types that are all but indistinguishable from their built-in counterparts, yet are substantially more flexible. A judiciously chosen and carefully crafted set of classes — one that automatically handles memory management, aliasing, initialization and clean-up, type conversions, and all the other conundrums that are the bane of software developers — can make application programming easy, intuitive, efficient, and nearly error-free. It isn't unduly difficult to write effective C++ programs, <I>if</I> you know how to do <NOBR>it.<SCRIPT>create_link(3);</SCRIPT>
</NOBR></P>
<P><A NAME="dingp4"></A><A NAME="3450"></A>
Used without discipline, C++ can lead to code that is incomprehensible, unmaintainable, inextensible, inefficient, and just plain <NOBR>wrong.<SCRIPT>create_link(4);</SCRIPT>
</NOBR></P>
<P><A NAME="dingp5"></A><A NAME="1711"></A>
The trick is to discover those aspects of C++ that are likely to trip you up and to learn how to avoid them. That is the purpose of this book. I assume you already know C++ as a <I>language</I> and that you have some experience in its use. What I provide here is a guide to using the language <I>effectively</I>, so that your software is comprehensible, maintainable, extensible, efficient, and likely to behave as you <NOBR>expect.<SCRIPT>create_link(5);</SCRIPT>
</NOBR></P>
<P><A NAME="dingp6"></A><A NAME="1712"></A>
The advice I proffer falls into two broad categories: general design strategies, and the nuts and bolts of specific language <NOBR>features.<SCRIPT>create_link(6);</SCRIPT>
</NOBR></P>
<P><A NAME="dingp7"></A><A NAME="1713"></A>
<A NAME="p2"></A>The design discussions concentrate on how to choose between different approaches to accomplishing something in C++. How do you choose between inheritance and templates? Between templates and generic pointers? Between public and private inheritance? Between private inheritance and layering? Between function overloading and parameter defaulting? Between virtual and nonvirtual functions? Between pass-by-value and pass-by-reference? It is important to get these decisions right at the outset, because an incorrect choice may not become apparent until much later in the development process, at which point its rectification is often difficult, time-consuming, demoralizing, and <NOBR>expensive.<SCRIPT>create_link(7);</SCRIPT>
</NOBR></P>
<P><A NAME="dingp8"></A><A NAME="1714"></A>
Even when you know exactly what you want to do, getting things just right can be tricky. What's the proper return type for the assignment operator? How should <CODE>operator</CODE> <CODE>new</CODE> behave when it can't find enough memory? When should a destructor be virtual? How should you write a member initialization list? It's crucial to sweat details like these, because failure to do so almost always leads to unexpected, possibly mystifying, program behavior. More importantly, the aberrant behavior may not be immediately apparent, giving rise to the specter of code that passes through quality control while still harboring a variety of undetected bugs — ticking time-bombs just waiting to go <NOBR>off.<SCRIPT>create_link(8);</SCRIPT>
</NOBR></P>
<P><A NAME="dingp9"></A><A NAME="1715"></A>
This is not a book that must be read cover to cover to make any sense. You need not even read it front to back. The material is broken down into 50 Items, each of which stands more or less on its own. Frequently, however, one Item will refer to others, so one way to read the book is to start with a particular Item of interest and then follow the references to see where they lead <NOBR>you.<SCRIPT>create_link(9);</SCRIPT>
</NOBR></P>
<P><A NAME="dingp10"></A><A NAME="1716"></A>
The Items are grouped into general topic areas, so if you are interested in discussions related to a particular issue, such as memory management or object-oriented design, you can start with the relevant section and either read straight through or start jumping around from there. You will find, however, that all of the material in this book is pretty fundamental to effective C++ programming, so almost everything is eventually related to everything else in one way or <NOBR>another.<SCRIPT>create_link(10);</SCRIPT>
</NOBR></P>
<P><A NAME="dingp11"></A><A NAME="20188"></A>
This is not a reference book for C++, nor is it a way for you to learn the language from scratch. For example, I'm eager to tell you all about the gotchas in writing your own <CODE>operator</CODE> <CODE>new</CODE> (see Items <A HREF="./EI7_FR.HTM#1894" TARGET="_top">7</A>-<A HREF="./EI10_FR.HTM#1986" TARGET="_top">10</A>), but I assume you can go elsewhere to discover that that function must return a <CODE>void*</CODE> and its first argument must be of type <CODE>size_t</CODE>. There are a number of introductory books on C++ that contain information such as <NOBR>that.<SCRIPT>create_link(11);</SCRIPT>
</NOBR></P>
<P><A NAME="dingp12"></A><A NAME="1718"></A>
<A NAME="p3"></A>The purpose of <I>this</I> book is to highlight those aspects of C++ programming that are usually treated superficially (if at all). Other books describe the different parts of the language. This book tells you how to combine those parts so you end up with effective programs. Other books tell you how to get your programs to compile. This book tells you how to avoid problems that compilers won't tell you <NOBR>about.<SCRIPT>create_link(12);</SCRIPT>
</NOBR></P>
<P><A NAME="dingp13"></A><A NAME="1719"></A>
Like most languages, C++ has a rich folklore that is usually passed from programmer to programmer as part of the language's grand oral tradition. This book is my attempt to record some of that accumulated wisdom in a more accessible <NOBR>form.<SCRIPT>create_link(13);</SCRIPT>
</NOBR></P>
<P><A NAME="dingp14"></A><A NAME="1720"></A>
At the same time, this book limits itself to legitimate, <I>portable</I>, C++. Only language features in the <NOBR><FONT COLOR="#FF0000" SIZE="-2"><B>°</B></FONT><A HREF="http://www.awl.com/cseng/cgi-bin/cdquery.pl?name=cstandard" onMouseOver="self.status='ISO/ANSI Language Standard'; return true" onMouseOut="self.status = self.defaultStatus" TARGET="_top">ISO/ANSI</NOBR> language standard</A> (see <A HREF="../MEC/MI35_FR.HTM#5473" TARGET="_top">Item M35</A>) have been used here. In this book, portability is a key concern, so if you're looking for implementation-dependent hacks and kludges, this is not the place to find <NOBR>them.<SCRIPT>create_link(14);</SCRIPT>
</NOBR></P>
<P><A NAME="dingp15"></A><A NAME="1726"></A>
Alas, C++ as described by the standard is sometimes different from the C++ supported by your friendly neighborhood compiler vendors. As a result, when I point out places where relatively new language features are useful, I also show you how to produce effective software in their absence. After all, it would be foolish to labor in ignorance of what the future is sure to bring, but by the same token, you can't just put your life on hold until the latest, greatest, be-all-and-end-all C++ compilers appear on your computer. You've got to work with the tools available to you, and this book helps you do just <NOBR>that.<SCRIPT>create_link(15);</SCRIPT>
</NOBR></P>
<P><A NAME="dingp16"></A><A NAME="12776"></A>
Notice that I refer to <i>compilers</i> — 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, i.e., from new features supported by only one vendor. Such features are often poorly implemented (buggy or slow — frequently both), and upon their introduction, the C++ community lacks experience to advise you in their proper application. Blazing trails can be exciting, but when your goal is producing reliable code, it's often best to let others do the bushwhacking for <NOBR>you.<SCRIPT>create_link(16);</SCRIPT>
</NOBR></P>
<P><A NAME="dingp17"></A><A NAME="1728"></A>
One thing you will <i>not</i> find in this book is the C++ Gospel, the One True Path to perfect C++ software. Each of the 50 Items in this book provides guidance on how to come up with better designs, how to avoid common problems, or how to achieve greater efficiency, but none of the Items is universally applicable. Software design and implementa<A NAME="p4"></A>tion is a complex task, one invariably colored by the constraints of the hardware, the operating system, and the application, so the best I can do is provide <I>guidelines</I> for creating better <NOBR>programs.<SCRIPT>create_link(17);</SCRIPT>
</NOBR></P>
<P><A NAME="dingp18"></A><A NAME="1729"></A>
If you follow all the guidelines all the time, you are unlikely to fall into the most common traps surrounding C++, but guidelines, by their very nature, have exceptions. That's why each Item has an explanation. The explanations are the most important part of the book. Only by understanding the rationale behind an Item can you reasonably determine whether it applies to the software you are developing and to the unique constraints under which you <NOBR>toil.<SCRIPT>create_link(18);</SCRIPT>
</NOBR></P>
<P><A NAME="dingp19"></A><A NAME="1730"></A>
The best use of this book, then, is to gain insight into how C++ behaves, why it behaves that way, and how to use its behavior to your advantage. Blind application of the Items in this book is clearly inappropriate, but at the same time, you probably shouldn't violate any of the guidelines without having a good reason for doing <NOBR>so.<SCRIPT>create_link(19);</SCRIPT>
</NOBR></P>
<P><A NAME="dingp20"></A><A NAME="1731"></A>
There's no point in getting hung up on terminology in a book like this; that form of sport is best left to language lawyers. However, there is a small C++ vocabulary that everybody should understand. The following terms crop up often enough that it is worth making sure we agree on what they <NOBR>mean.<SCRIPT>create_link(20);</SCRIPT>
</NOBR></P>
<P><A NAME="dingp21"></A><A NAME="1732"></A>
A <I>declaration</I> tells compilers about the name and type of an object, function, class, or template, but it omits certain details. These are <NOBR>declarations:<SCRIPT>create_link(21);</SCRIPT>
</NOBR></P>
<A NAME="1733"></A>
<UL><PRE>extern int x; // object declaration
</PRE><A NAME="14198"></A>
<PRE>int numDigits(int number); // function declaration
</PRE><A NAME="14200"></A>
<PRE>class Clock; // class declaration
</PRE><A NAME="13390"></A>
<PRE>template<class T>
class SmartPointer; // template declaration
</PRE>
</UL>
<P><A NAME="dingp22"></A><A NAME="1735"></A>
A <I>definition</I>, on the other hand, provides compilers with the details. For an object, the definition is where compilers allocate memory for the object. For a function or a function template, the definition provides the code body. For a class or a class template, the definition lists the members of the class or <NOBR>template:<SCRIPT>create_link(22);</SCRIPT>
</NOBR></P>
<A NAME="13417"></A>
<UL><PRE>int x; // object definition
</PRE><A NAME="13423"></A>
<PRE>int numDigits(int number) // function definition
{ // (this function returns
int digitsSoFar = 1; // the number of digits in
// its parameter)
</PRE><A NAME="13427"></A>
<PRE> <A NAME="p5"></A>if (number < 0) {
number = -number;
++digitsSoFar;
}
</PRE><A NAME="13432"></A>
<PRE> while (number /= 10) ++digitsSoFar;
</PRE><A NAME="13433"></A>
<PRE> return digitsSoFar;
}
</PRE><A NAME="13421"></A>
<PRE>class Clock { // class definition
public:
Clock();
~Clock();
</PRE><A NAME="12306"></A>
<PRE> int hour() const;
int minute() const;
int second() const;
</PRE><A NAME="12677"></A>
<PRE> ...
</PRE><A NAME="12678"></A>
<PRE>};
</PRE><A NAME="13393"></A>
<PRE>template<class T>
class SmartPointer { // template definition
public:
SmartPointer(T *p = 0);
~SmartPointer();
</PRE><A NAME="13396"></A>
<PRE> T * operator->() const;
T& operator*() const;
</PRE><A NAME="13400"></A>
<PRE> ...
</PRE><A NAME="13401"></A>
<PRE>};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -