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

📄 e.htm

📁 一个非常适合初学者入门的有关c++的文档
💻 HTM
📖 第 1 页 / 共 5 页
字号:
<P><A NAME="dingp43"></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="#1894"onMouseOver = "self.status = '7'; return true" onMouseOut = "self.status = self.defaultStatus">7</A>-<A HREF="#1986"onMouseOver = "self.status = '10'; return true" onMouseOut = "self.status = self.defaultStatus">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(43);</SCRIPT>

</NOBR></P>

<P><A NAME="dingp44"></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(44);</SCRIPT>

</NOBR></P>

<P><A NAME="dingp45"></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(45);</SCRIPT>

</NOBR></P>

<P><A NAME="dingp46"></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>&deg;</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/M_FR.HTM#5473" TARGET="_top" onMouseOver = "self.status = 'Item M35'; return true" onMouseOut = "self.status = self.defaultStatus">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(46);</SCRIPT>

</NOBR></P>

<P><A NAME="dingp47"></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(47);</SCRIPT>

</NOBR></P>

<P><A NAME="dingp48"></A><A NAME="12776"></A>
Notice that I refer to <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, i.e., 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 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(48);</SCRIPT>

</NOBR></P>

<P><A NAME="dingp49"></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(49);</SCRIPT>

</NOBR></P>

<P><A NAME="dingp50"></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(50);</SCRIPT>

</NOBR></P>

<P><A NAME="dingp51"></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(51);</SCRIPT>

</NOBR></P>

<P><A NAME="dingp52"></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(52);</SCRIPT>

</NOBR></P>

<P><A NAME="dingp53"></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(53);</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&lt;class T&gt;
class SmartPointer;                    // template declaration
</PRE>
</UL>

<P><A NAME="dingp54"></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(54);</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 &lt; 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&lt;class T&gt;
class SmartPointer {                    // template definition
public:
  SmartPointer(T *p = 0);
  ~SmartPointer();
</PRE><A NAME="13396"></A>
<PRE>  T * operator-&gt;() const;
  T&amp; operator*() const;
</PRE><A NAME="13400"></A>
<PRE>  ...
</PRE><A NAME="13401"></A>
<PRE>};
</PRE>
</UL>

<P><A NAME="dingp55"></A><A NAME="12264"></A>
That brings us to constructors. A <I>default constructor</I> is one that can be called without any arguments. Such a constructor either has no parameters or has a default value for every parameter. You generally need a default constructor if you want to define arrays of <NOBR>objects:<SCRIPT>create_link(55);</SCRIPT>

⌨️ 快捷键说明

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