📄 ei50.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 | Item 50: Improve your understanding of C++</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 LANGUAGE="Javascript">
var dingbase = "EI50_DIR.HTM";
var dingtext = "Item E50, P";
if (self == top) {
top.location.replace(dingbase + this.location.hash);
}
</SCRIPT>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000" ONLOAD="setResize()">
<!-- SectionName="E50: Improve your understanding of C++." -->
<A NAME="8569"></A>
<DIV ALIGN="CENTER"><FONT SIZE="-1">Back to <A HREF="EI49_FR.HTM" TARGET="_top">Item 49: Familiarize yourself with the standard library.</A> <BR> Continue to <A HREF="./EIAFTRFR.HTM" TARGET="_top">Afterword</A></DIV></FONT>
<P><A NAME="dingp1"></A><FONT ID="eititle">Item 50: Improve your understanding of C++.</FONT><SCRIPT>create_link(1);</SCRIPT>
</P>
<A NAME="25193"></A>
<P><A NAME="dingp2"></A>
There's a lot of <I>stuff</I> in C++. C stuff. Overloading stuff. Object-oriented stuff. Template stuff. Exception stuff. Namespace stuff. Stuff, stuff, stuff! Sometimes it can be overwhelming. How do you make sense of all that <NOBR>stuff?<SCRIPT>create_link(2);</SCRIPT>
</NOBR></P>
<A NAME="25212"></A>
<P><A NAME="dingp3"></A>
It's not that hard once you understand the design goals that forged C++ into what it is. Foremost amongst those goals are the <NOBR>following:<SCRIPT>create_link(3);</SCRIPT>
</NOBR></P>
<UL><A NAME="25220"></A>
<A NAME="dingp4"></A><LI><A NAME="p233"></A><B>Compatibility with C.</B> Lots and lots of C exists, as do lots and lots of C programmers. C++ takes advantage of and builds on — er, I mean it "leverages" — that base.<SCRIPT>create_link(4);</SCRIPT>
<A NAME="25225"></A>
<A NAME="dingp5"></A><LI><B>Efficiency.</B> <NOBR><FONT COLOR="#FF0000" SIZE="-2"><B>°</B></FONT><A HREF="http://www.awl.com/cseng/cgi-bin/cdquery.pl?name=bjarne" onMouseOver = "self.status = 'Bjarne Stroustrup'; return true" onMouseOut = "self.status = self.defaultStatus" TARGET="_top">Bjarne</NOBR> Stroustrup</A>, the designer and first implementer of C++, knew from the outset that the C programmers he hoped to win over wouldn't look twice if they had to pay a performance penalty for switching languages. As a result, he made sure C++ was competitive with C when it came to efficiency — like within 5%.<SCRIPT>create_link(5);</SCRIPT>
<A NAME="223075"></A>
<A NAME="dingp6"></A><LI><B>Compatibility with traditional tools and environments.</B> Fancy development environments run here and there, but compilers, linkers, and editors run almost everywhere. C++ is designed to work in environments from mice to mainframes, so it brings along as little baggage as possible. You want to port C++? You port a <i>language</i> and take advantage of existing tools on the target platform. (However, it is often possible to provide a better implementation if, for example, the linker can be modified to address some of the more demanding aspects of inlining and templates.)<SCRIPT>create_link(6);</SCRIPT>
<A NAME="25694"></A>
<A NAME="dingp7"></A><LI><B>Applicability to real problems.</B> C++ wasn't designed to be a nice, pure language, good for teaching students how to program, it was designed to be a powerful tool for professional programmers solving real problems in diverse domains. The real world has some rough edges, so it's no surprise there's the occasional scratch marring the finish of the tools on which the pros rely.<SCRIPT>create_link(7);</SCRIPT>
</UL></P>
<A NAME="25701"></A>
<P><A NAME="dingp8"></A>
These goals explain a multitude of language details that might otherwise merely chafe. Why do implicitly-generated copy constructors and assignment operators behave the way they do, especially for pointers (see Items <A HREF="./EI11_FR.HTM#2042" TARGET="_top">11</A> and <A HREF="./EI45_FR.HTM#8160" TARGET="_top">45</A>)? Because that's how C copies and assigns <CODE>struct</CODE>s, and compatibility with C is important. Why aren't destructors automatically virtual (see <A HREF="./EI14_FR.HTM#223029" TARGET="_top">Item 14</A>), and why must implementation details appear in class definitions (see <A HREF="./EI34_FR.HTM#6793" TARGET="_top">Item 34</A>)? Because doing otherwise would impose a performance penalty, and efficiency is important. Why can't C++ detect initialization dependencies between non-local static objects (see <A HREF="./EI47_FR.HTM#8299" TARGET="_top">Item 47</A>)? Because C++ supports separate translation (i.e., the ability to compile source modules separately, then link several object files together to form an executable), relies on existing linkers, and doesn't mandate the existence of program databases. As a result, C++ compilers almost never know everything about an entire program. Finally, why doesn't C++ free programmers from tiresome duties like memory management (see Items <A HREF="./EI5_FR.HTM#1869" TARGET="_top">5</A>-<A HREF="./EI10_FR.HTM#1986" TARGET="_top">10</A>) and low-level pointer manipulations? Because some programmers need those capabilities, and the needs of real programmers are of paramount <NOBR>importance.<SCRIPT>create_link(8);</SCRIPT>
</NOBR></P>
<A NAME="25423"></A>
<P><A NAME="dingp9"></A>
This barely hints at how the design goals behind C++ shape the behavior of the language. To cover everything would take an entire book, so <A NAME="p234"></A>it's convenient that Stroustrup wrote one. That book is <NOBR><FONT COLOR="#FF0000" SIZE="-2"><B>°</B></FONT><A HREF="http://www.awl.com/cseng/cgi-bin/cdquery.pl?name=desec" onMouseOver = "self.status = 'The Design and Evolution of C++'; return true" onMouseOut = "self.status = self.defaultStatus" TARGET="_top"><I>The</NOBR> Design and Evolution of C++</I></A> (Addison-Wesley, 1994), sometimes known as simply "D&E." Read it, and you'll see what features were added to C++, in what order, and why. You'll also learn about features that were rejected, and why. You'll even get the inside story on how the <CODE>dynamic_cast</CODE> feature (see Items <A HREF="./EI39_FR.HTM#7269" TARGET="_top">39</A> and <A HREF="../MEC/MI2_FR.HTM#77216" TARGET="_top">M2</A>) was considered, rejected, reconsidered, then accepted — and why. If you're having trouble making sense of C++, D&E should dispel much of your <NOBR>confusion.<SCRIPT>create_link(9);</SCRIPT>
</NOBR></P>
<A NAME="25475"></A>
<P><A NAME="dingp10"></A>
<I>The Design and Evolution of C++</I> offers a wealth of insights into how C++ came to be what it is, but it's nothing like a formal specification for the language. For that you must turn to 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 = 'The latest publicly-available version of the C++ standard'; return true" onMouseOut = "self.status = self.defaultStatus" TARGET="_top">international</NOBR> standard for C++</A>, an impressive exercise in formalese running some 700 pages. There you can read such riveting prose as <NOBR>this:<SCRIPT>create_link(10);</SCRIPT>
</NOBR></P>
<A NAME="25476"></A>
<P><A NAME="dingp11"></A>
<BLOCKQUOTE>A virtual function call uses the default arguments in the declaration of the virtual function determined by the static type of the pointer or reference denoting the object. An overriding function in a derived class does not acquire default arguments from the function it overrides.<SCRIPT>create_link(11);</SCRIPT>
</BLOCKQUOTE></P>
<A NAME="25182"></A>
<P><A NAME="dingp12"></A>
This paragraph is the basis for <A HREF="./EI38_FR.HTM#177948" TARGET="_top">Item 38</A> ("Never redefine an inherited default parameter value"), but I hope my treatment of the topic is somewhat more accessible than the text <NOBR>above.<SCRIPT>create_link(12);</SCRIPT>
</NOBR></P>
<A NAME="25614"></A>
<P><A NAME="dingp13"></A>
The standard is hardly bedtime reading, but it's your best recourse — your <I>standard</I> recourse — if you and someone else (a compiler vendor, say, or a developer of some other tool that processes source code) disagree on what is and isn't C++. The whole purpose of a standard is to provide definitive information that settles arguments like <NOBR>that.<SCRIPT>create_link(13);</SCRIPT>
</NOBR></P>
<A NAME="223845"></A>
<P><A NAME="dingp14"></A>
The standard's official title is a mouthful, but if you need to know it, you need to know it. Here it is: <I>International Standard for Information Systems—Programming Language C++</I>. It's published by Working Group 21 of the <NOBR><FONT COLOR="#FF0000" SIZE="-2"><B>°</B></FONT><A HREF="http://www.awl.com/cseng/cgi-bin/cdquery.pl?name=iso" onMouseOver = "self.status = 'ISO Home Page'; return true" onMouseOut = "self.status = self.defaultStatus" TARGET="_top">International</NOBR> Organization for Standardization (ISO)</A>. (If you insist on being picky about it, it's really published by — I am not making this up — ISO/IEC JTC1/SC22/WG21.) You can order a copy of the official standard from your national standards body (in the United States, that's ANSI, the <NOBR><FONT COLOR="#FF0000" SIZE="-2"><B>°</B></FONT><A HREF="http://www.awl.com/cseng/cgi-bin/cdquery.pl?name=ansi" onMouseOver = "self.status = 'ANSI Home Page'; return true" onMouseOut = "self.status = self.defaultStatus" TARGET="_top">American</NOBR> National Standards Institute</A>), but copies of late drafts of the standard — which are quite similar (though not identical) to the final document — are freely available on the World Wide Web. A good place to look for a copy is at <NOBR><FONT COLOR="#FF0000" SIZE="-2"><B>°</B></FONT><A HREF="http://www.awl.com/cseng/cgi-bin/cdquery.pl?name=cygdsc" onMouseOver = "self.status = 'The Cygnus Solutions Draft Standard C++ Home Page'; return true" onMouseOut = "self.status = self.defaultStatus" TARGET="_top">the</NOBR> Cygnus Solutions Draft Standard C++ Page</A>, but given the pace of change in cyberspace, don't be surprised if this link is broken by the time you try it. If it is, your favorite Web search engine will doubtless turn up a URL that <NOBR>works.<SCRIPT>create_link(14);</SCRIPT>
</NOBR></P>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -