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

📄 m.htm

📁 一个非常适合初学者入门的有关c++的文档
💻 HTM
📖 第 1 页 / 共 5 页
字号:
</UL>
<A NAME="31095"></A>
<P><A NAME="dingp93"></A>
Well, this is evil, pure and simple. The results are undefined (compilers can generate output to do anything they like), and people who write this kind of code should be shunned until they agree to cease and desist. If you have to worry about things like this in your software, you're probably best off avoiding references entirely. Either that or finding a better class of programmers to work with. We'll henceforth ignore the possibility that a reference can be <NOBR>"null."<SCRIPT>create_link(93);</SCRIPT>

</NOBR></P><A NAME="65889"></A>
<P><A NAME="dingp94"></A>
Because a reference must refer to an object, C++ requires that references be <NOBR>initialized:<SCRIPT>create_link(94);</SCRIPT>

</NOBR></P><A NAME="31102"></A>
<UL><PRE>string&amp; rs;             // error! References must
                        // be initialized
<A NAME="31104"></A>
string s("xyzzy");
<A NAME="31105"></A>
string&amp; rs = s;         // okay, rs refers to s</PRE>
</UL>
<A NAME="31106"></A>
<P><A NAME="dingp95"></A>
Pointers are subject to no such <NOBR>restriction:<SCRIPT>create_link(95);</SCRIPT>

</NOBR></P>
<A NAME="31107"></A>
<UL><PRE>string *ps;             // uninitialized pointer:
                        // valid but risky</PRE>
</UL>
						<A NAME="65942"></A>
<P><A NAME="dingp96"></A>
The fact that there is no such thing as a null reference implies that it can be more efficient to use references than to use pointers. That's because there's no need to test the validity of a reference before using <NOBR>it:<SCRIPT>create_link(96);</SCRIPT>

</NOBR></P><A NAME="31066"></A>
<UL><PRE>void printDouble(const double&amp; rd)
{
    cout &lt;&lt; rd;         // no need to test rd; it
}                       // must refer to a double</PRE>
</UL>
<A NAME="31130"></A>
<P><A NAME="dingp97"></A>
Pointers, on the other hand, should generally be tested against <NOBR>null:<SCRIPT>create_link(97);</SCRIPT>

</NOBR></P><A NAME="31085"></A>
<UL><PRE>void printDouble(const double *pd)
{
  if (pd) {             // check for null pointer
    cout &lt;&lt; *pd;
  }
}</PRE>
</UL>
<A NAME="31064"></A>
<A NAME="p11"></A><P><A NAME="dingp98"></A>
Another important difference between pointers and references is that pointers may be reassigned to refer to different objects. A reference, however, <I>always</I> refers to the object with which it is <NOBR>initialized:<SCRIPT>create_link(98);</SCRIPT>

</NOBR></P><A NAME="31164"></A>
<UL><PRE>string s1("Nancy");
string s2("Clancy");
<A NAME="31165"></A>
string&amp; rs = s1;         // rs refers to s1
<A NAME="31167"></A>
string *ps = &amp;s1;        // ps points to s1<A NAME="31186"></A>

rs = s2;                 // rs still refers to s1,
                         // but s1's value is now
                         // "Clancy"
<A NAME="31187"></A>
ps = &amp;s2;                // ps now points to s2;
                         // s1 is unchanged</PRE>
</UL>
<A NAME="31199"></A><P><A NAME="dingp99"></A>
In general, you should use a pointer whenever you need to take into account the possibility that there's nothing to refer to (in which case you can set the pointer to null) or whenever you need to be able to refer to different things at different times (in which case you can change where the pointer points). You should use a reference whenever you know there will always be an object to refer to and you also know that once you're referring to that object, you'll never want to refer to anything <NOBR>else.<SCRIPT>create_link(99);</SCRIPT>

</NOBR></P>

<A NAME="31206"></A>
<P><A NAME="dingp100"></A>
There is one other situation in which you should use a reference, and that's when you're implementing certain operators. The most common example is <CODE>operator[]</CODE>. This operator typically needs to return something that can be used as the target of an <NOBR>assignment:<SCRIPT>create_link(100);</SCRIPT>

</NOBR></P><A NAME="31230"></A>
<UL><PRE>vector&lt;int&gt; v(10);       // create an int vector of size 10;
                         // vector is a template in the
                         // standard C++ library (see <A HREF="#5473">Item&nbsp;35</A>)</PRE>
</UL><A NAME="31250"></A>
<UL><PRE>v[5] = 10;               // the target of this assignment is
                         // the return value of operator[]
</PRE>
</UL><A NAME="31257"></A>
<P><A NAME="dingp101"></A>
If <CODE>operator[]</CODE> returned a pointer, this last statement would have to be written this <NOBR>way:<SCRIPT>create_link(101);</SCRIPT>

</NOBR></P><A NAME="31258"></A>
<UL><PRE>*v[5] = 10;
</PRE>
</UL><A NAME="31259"></A>
<P><A NAME="dingp102"></A>
But this makes it look like <CODE>v</CODE> is a vector of pointers, which it's not. For this reason, you'll almost always want <CODE>operator[]</CODE> to return a reference. (For an interesting exception to this rule, see <A HREF="#6074">Item&nbsp;30</A>.)<SCRIPT>create_link(102);</SCRIPT>

</P><A NAME="77177"></A>
<P><A NAME="dingp103"></A>
References, then, are the feature of choice when you <i>know</i> you have something to refer to, when you'll <I>never</I> want to refer to anything else, and when implementing operators whose syntactic requirements make the use of pointers undesirable. In all other cases, stick with <NOBR>pointers.<SCRIPT>create_link(103);</SCRIPT>

</NOBR></P>

<!-- SectionName="M2: Prefer C++-style casts" -->

<A NAME="p9"></A>

<A NAME="77216"></A><A NAME="p12"></A><DIV ALIGN="CENTER"><FONT SIZE="-1">Back to <A HREF="#11029">Item 1: Distinguish between pointers and references</A> <BR>Continue to <A HREF="#84818">Item 3: Never treat arrays polymorphically</A></FONT></DIV>

<P><A NAME="dingp104"></A><font ID="mititle">Item 2: &nbsp;Prefer C++-style casts.</font><SCRIPT>create_link(104);</SCRIPT>

</P>

<A NAME="77217"></A>
<A NAME="77218"></A>
<P><A NAME="dingp105"></A>
Consider the lowly cast. Nearly as much a programming pariah as the <CODE>goto</CODE>, it nonetheless endures, because when worse comes to worst and push comes to shove, casts can be necessary. Casts are especially necessary when worse comes to worst and push comes to <NOBR>shove.<SCRIPT>create_link(105);</SCRIPT>

</NOBR></P><A NAME="10133"></A>
<P><A NAME="dingp106"></A>
Still, C-style casts are not all they might be. For one thing, they're rather crude beasts, letting you cast pretty much any type to pretty much any other type. It would be nice to be able to specify more precisely the purpose of each cast. There is a great difference, for example, between a cast that changes a pointer-to-<CODE>const</CODE>-object into a pointer-to-non-<CODE>const</CODE>-object (i.e., a cast that changes only the <CODE>const</CODE>ness of an object) and a cast that changes a pointer-to-base-class-object into a pointer-to-derived-class-object (i.e., a cast that completely changes an object's type). Traditional C-style casts make no such distinctions. (This is hardly a surprise. C-style casts were designed for C, not <NOBR>C++.)<SCRIPT>create_link(106);</SCRIPT>

</NOBR></P><A NAME="10134"></A>
<P><A NAME="dingp107"></A>
A second problem with casts is that they are hard to find. Syntactically, casts consist of little more than a pair of parentheses and an identifier, and parentheses and identifiers are used everywhere in C++. This makes it tough to answer even the most basic cast-related questions, questions like, "Are any casts used in this program?" That's because human readers are likely to overlook casts, and tools like <CODE>grep</CODE> cannot distinguish them from non-cast constructs that are syntactically <NOBR>similar.<SCRIPT>create_link(107);</SCRIPT>

</NOBR></P><A NAME="77185"></A>
<P><A NAME="dingp108"></A>
C++ addresses the shortcomings of C-style casts by introducing four new cast operators, <CODE>static_cast</CODE>, <CODE>const_cast</CODE>, <CODE>dynamic_cast</CODE>, and <CODE>reinterpret_cast</CODE>. For most purposes, all you need to know about these operators is that what you are accustomed to writing like <NOBR>this,<SCRIPT>create_link(108);</SCRIPT>

</NOBR></P><A NAME="77186"></A>
<UL><PRE>(<I>type</I>) <I>expression</I>
</PRE>
</UL><A NAME="77187"></A>
<A NAME="dingp109"></A>you should now generally write like this:<SCRIPT>create_link(109);</SCRIPT>

<A NAME="77188"></A>
<UL><PRE>static_cast&lt;<I>type</I>&gt;(<I>expression</I>)
</PRE>
</UL><A NAME="77189"></A>
<P><A NAME="dingp110"></A>
For example, suppose you'd like to cast an <CODE>int</CODE> to a <CODE>double</CODE> to force an expression involving <CODE>int</CODE>s to yield a floating point value. Using C-style casts, you could do it like <NOBR>this:<SCRIPT>create_link(110)

⌨️ 快捷键说明

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