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

📄 ch09.htm

📁 一本好的VC学习书,本人就是使用这本书开始学习的vc,希望能对大家有帮助
💻 HTM
📖 第 1 页 / 共 5 页
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN"><HTML><HEAD><!-- This document was created from RTF source by rtftohtml version 3.0.1 -->	<META NAME="GENERATOR" Content="Symantec Visual Page 1.0">	<META HTTP-EQUIV="Content-Type" CONTENT="text/html;CHARSET=iso-8859-1">	<TITLE>Teach Yourself C++ in 21 Days</TITLE></HEAD><BODY TEXT="#000000" BGCOLOR="#FFFFFF"><H1 ALIGN="CENTER"><A HREF="ch08.htm" tppabs="http://www.mcp.com/814147200/0-672/0-672-31070-8/htm/ch08.htm"><IMG SRC="BLANPREV.GIF" tppabs="http://www.mcp.com/814147200/0-672/0-672-31070-8/buttons/BLANPREV.GIF"WIDTH="37" HEIGHT="37" ALIGN="BOTTOM" BORDER="0"></A><A HREF="tppmsgs/msgs0.htm#1" tppabs="http://www.mcp.com/sams"><IMGSRC="BLANHOME.GIF" tppabs="http://www.mcp.com/814147200/0-672/0-672-31070-8/buttons/BLANHOME.GIF" WIDTH="37" HEIGHT="37" ALIGN="BOTTOM"BORDER="0"></A><A HREF="index.htm" tppabs="http://www.mcp.com/814147200/0-672/0-672-31070-8/index.htm"><IMG SRC="BLANTOC.GIF" tppabs="http://www.mcp.com/814147200/0-672/0-672-31070-8/buttons/BLANTOC.GIF"WIDTH="37" HEIGHT="37" ALIGN="BOTTOM" BORDER="0"></A><A HREF="ch10.htm" tppabs="http://www.mcp.com/814147200/0-672/0-672-31070-8/htm/ch10.htm"><IMG SRC="BLANNEXT.GIF" tppabs="http://www.mcp.com/814147200/0-672/0-672-31070-8/buttons/BLANNEXT.GIF"WIDTH="37" HEIGHT="37" ALIGN="BOTTOM" BORDER="0"></A></H1><H1></H1><UL>	<LI><A HREF="#Heading1">Day 9</A>	<UL>		<LI><A HREF="#Heading2">References</A>		<UL>			<LI><A HREF="#Heading3">What Is a Reference?</A>			<LI><A HREF="#Heading4">Listing 9.1. Creating and using references.</A>			<LI><A HREF="#Heading5">Using the Address of Operator &amp; on References</A>			<LI><A HREF="#Heading6">Listing 9.2. Taking the address of a reference</A>			<LI><A HREF="#Heading8">Listing 9.3. Assigning to a reference</A>			<LI><A HREF="#Heading10">What Can Be Referenced?</A>			<LI><A HREF="#Heading11">Listing 9.4. References to objects</A>			<LI><A HREF="#Heading13">References</A>			<LI><A HREF="#Heading14">Null Pointers and Null References</A>			<LI><A HREF="#Heading15">Passing Function Arguments by Reference</A>			<LI><A HREF="#Heading16">Listing 9.5. Demonstrating passing by value</A>			<UL>				<LI><A HREF="#Heading18">Making swap() Work with Pointers</A>			</UL>			<LI><A HREF="#Heading19">Listing 9.6. Passing by reference using pointers</A>			<UL>				<LI><A HREF="#Heading21">Implementing swap() with References</A>			</UL>			<LI><A HREF="#Heading22">Listing 9.7. swap() rewritten with references</A>			<LI><A HREF="#Heading24">Understanding Function Headers and Prototypes</A>			<LI><A HREF="#Heading25">Returning Multiple Values</A>			<LI><A HREF="#Heading26">Listing 9.8. Returning values with pointers</A>			<UL>				<LI><A HREF="#Heading28">Returning Values by Reference</A>			</UL>			<LI><A HREF="#Heading29">Listing 9.9.</A>			<LI><A HREF="#Heading30">Listing 9.8 rewritten using references.</A>			<LI><A HREF="#Heading31">Passing by Reference for Efficiency</A>			<LI><A HREF="#Heading32">Listing 9.10. Passing objects by reference</A>			<UL>				<LI><A HREF="#Heading34">Passing a const Pointer</A>			</UL>			<LI><A HREF="#Heading35">Listing 9.11. Passing const pointers</A>			<UL>				<LI><A HREF="#Heading37">References as an Alternative</A>			</UL>			<LI><A HREF="#Heading38">Listing 9.12. Passing references to objects</A>			<LI><A HREF="#Heading40">const References</A>			<LI><A HREF="#Heading41">When to Use References and When to Use Pointers</A>			<LI><A HREF="#Heading42">Mixing References and Pointers</A>			<LI><A HREF="#Heading43">Dont Return a Reference to an Object that Isnt in Scope!</A>			<LI><A HREF="#Heading44">Listing 9.13. Returning a reference to a non-existent object</A>			<LI><A HREF="#Heading46">Returning a Reference to an Object on the Hea</A>			<LI><A HREF="#Heading48">Listing 9.14. Memory leaks</A>			<LI><A HREF="#Heading50">Pointer, Pointer, Who Has the Pointer?</A>			<LI><A HREF="#Heading51">Summary</A>			<LI><A HREF="#Heading52">Q&amp;A</A>			<LI><A HREF="#Heading53">Workshop</A>			<UL>				<LI><A HREF="#Heading54">Quiz</A>				<LI><A HREF="#Heading55">Exercises</A>			</UL>		</UL>	</UL></UL><P><HR><BR><H2 ALIGN="CENTER"><A NAME="Heading1"></A><FONT COLOR="#000077">Day 9</FONT></H2><H2 ALIGN="CENTER"><A NAME="Heading2"></A><FONT COLOR="#000077">References</FONT></H2><P>Yesterday you learned how to use pointers to manipulate objects on the free storeand how to refer to those objects indirectly. References, the topic of today's chapter,give you almost all the power of pointers but with a much easier syntax. Today youlearn the following<UL>	<LI>What references are.	<P>	<LI>How references differ from pointers.	<P>	<LI>How to create references and use them.	<P>	<LI>What the limitations of references are.	<P>	<LI>How to pass values and objects into and out of functions by reference.</UL><H3 ALIGN="CENTER"><A NAME="Heading3"></A><FONT COLOR="#000077">What Is a Reference?</FONT></H3><P>A reference is an alias; when you create a reference, you initialize it with thename of another object, the target. From that moment on, the reference acts as analternative name for the target, and anything you do to the reference is really doneto the target.</P><P>You create a reference by writing the type of the target object, followed by thereference operator (<TT>&amp;</TT>), followed by the name of the reference. Referencescan use any legal variable name, but for this book we'll prefix all reference nameswith &quot;r.&quot; Thus, if you have an integer variable named <TT>someInt</TT>,you can make a reference to that variable by writing the following:</P><PRE><FONT COLOR="#0066FF">int &amp;rSomeRef = someInt;</FONT></PRE><P>This is read as &quot;<TT>rSomeRef</TT> is a reference to an integer that is initializedto refer to <TT>someInt</TT>.&quot; Listing 9.1 shows how references are createdand used.<BLOCKQUOTE>	<P><HR><FONT COLOR="#000077"><B>NOTE:</B></FONT><FONT COLOR="#0000AA"><B> </B></FONT>Note	that the reference operator (<TT>&amp;</TT>) is the same symbol as the one used for	the address of the operator. These are not the same operators, however, though clearly	they are related. <HR></BLOCKQUOTE><P><A NAME="Heading4"></A><FONT SIZE="4" COLOR="#000077"><B>Listing 9.1. Creatingand using references.</B></FONT><PRE><FONT COLOR="#0066FF">1:    //Listing 9.12:    // Demonstrating the use of References3:4:    #include &lt;iostream.h&gt;5:6:    int main()7:    {8:         int  intOne;9:         int &amp;rSomeRef = intOne;10:11:        intOne = 5;12:        cout &lt;&lt; &quot;intOne: &quot; &lt;&lt; intOne &lt;&lt; endl;13:        cout &lt;&lt; &quot;rSomeRef: &quot; &lt;&lt; rSomeRef &lt;&lt; endl;14:15:        rSomeRef = 7;16:        cout &lt;&lt; &quot;intOne: &quot; &lt;&lt; intOne &lt;&lt; endl;17:        cout &lt;&lt; &quot;rSomeRef: &quot; &lt;&lt; rSomeRef &lt;&lt; endl;18:   return 0;<TT>19: }</TT></FONT><FONT COLOR="#0066FF">Output: intOne: 5rSomeRef: 5intOne: 7rSomeRef: 7</FONT></PRE><P><FONT COLOR="#000077"><TT><B>Anaylsis: </B></TT></FONT>On line 8, a local <TT>int</TT>variable, <TT>intOne</TT>, is declared. On line 9, a reference to an <TT>int</TT>,<TT>rSomeRef</TT>, is declared and initialized to refer to <TT>intOne</TT>. If youdeclare a reference, but don't initialize it, you will get a compile-time error.References must be initialized.<BR>On line 11, <TT>intOne</TT> is assigned the value <TT>5</TT>. On lines 12 and 13,the values in <TT>intOne</TT> and <TT>rSomeRef</TT> are printed, and are, of course,the same.</P><P>On line 15, <TT>7</TT> is assigned to <TT>rSomeRef</TT>. Since this is a reference,it is an alias for <TT>intOne</TT>, and thus the <TT>7</TT> is really assigned to<TT>intOne</TT>, as is shown by the printouts on lines 16 and 17.<H3 ALIGN="CENTER"><A NAME="Heading5"></A><FONT COLOR="#000077">Using the Addressof Operator &amp; on References</FONT></H3><P>If you ask a reference for its address, it returns the address of its target.That is the nature of references. They are aliases for the target. Listing 9.2 demonstratesthis.</P><P><A NAME="Heading6"></A><FONT SIZE="4" COLOR="#000077"><B>Listing 9.2. Taking theaddress of a reference</B></FONT><FONT SIZE="2" COLOR="#000077"><B>.</B></FONT><PRE><FONT COLOR="#0066FF">1:    //Listing 9.22:    // Demonstrating the use of References3:4:    #include &lt;iostream.h&gt;5:6:    int main()7:    {8:        int  intOne;9:        int &amp;rSomeRef = intOne;10:11:       intOne = 5;12:       cout &lt;&lt; &quot;intOne: &quot; &lt;&lt; intOne &lt;&lt; endl;13:       cout &lt;&lt; &quot;rSomeRef: &quot; &lt;&lt; rSomeRef &lt;&lt; endl;14:15:       cout &lt;&lt; &quot;&amp;intOne: &quot;  &lt;&lt; &amp;intOne &lt;&lt; endl;16:       cout &lt;&lt; &quot;&amp;rSomeRef: &quot; &lt;&lt; &amp;rSomeRef &lt;&lt; endl;17:18:   return 0;<TT>19: }</TT></FONT><FONT COLOR="#0066FF">Output: intOne: 5rSomeRef: 5&amp;intOne:  0x3500&amp;rSomeRef: 0x3500</FONT></PRE><BLOCKQUOTE>	<P><HR><FONT COLOR="#000077"><B>NOTE:</B></FONT><B> </B>Your output may differ on the last	two lines. <HR></BLOCKQUOTE><P><HR><FONT COLOR="#000077"><TT><B>Anaylsis:</B></TT></FONT><TT><B> </B></TT>Once again<TT>rSomeRef</TT> is initialized as a reference to <TT>intOne</TT>. This time theaddresses of the two variables are printed, and they are identical. C++ gives youno way to access the address of the reference itself because it is not meaningful,as it would be if you were using a pointer or other variable. References are initializedwhen created, and always act as a synonym for their target, even when the addressof operator is applied.<BR>For example, if you have a class called <TT>President</TT>, you might declare aninstance of that class as follows:</P><PRE><FONT COLOR="#0066FF">President  William_Jefferson_Clinton;</FONT></PRE><P>You might then declare a reference to <TT>President</TT> and initialize it withthis object:</P><PRE><FONT COLOR="#0066FF">President &amp;Bill_Clinton = William_Jefferson_Clinton;</FONT></PRE><P>There is only one <TT>President</TT>; both identifiers refer to the same objectof the same class. Any action you take on <TT>Bill_Clinton</TT> will be taken on<TT>William_Jefferson_Clinton</TT> as well.</P><P>Be careful to distinguish between the <TT>&amp;</TT> symbol on line 9 of Listing9.2, which declares a reference to <TT>int</TT> named <TT>rSomeRef</TT>, and the<TT>&amp;</TT> symbols on lines 15 and 16, which return the addresses of the integervariable <TT>intOne</TT> and the reference <TT>rSomeRef</TT>.</P><P>Normally, when you use a reference, you do not use the address of operator. Yousimply use the reference as you would use the target variable. This is shown on line13.</P><P>Even experienced C++ programmers, who know the rule that references cannot bereassigned and are always aliases for their target, can be confused by what happenswhen you try to reassign a reference. What appears to be a reassignment turns outto be the assignment of a new value to the target. Listing 9.3 illustrates this fact.</P><P><A NAME="Heading8"></A><FONT SIZE="4" COLOR="#000077"><B>Listing 9.3. Assigningto a reference.</B></FONT><PRE><FONT COLOR="#0066FF">1:     //Listing 9.32:      //Reassigning a reference3:4:      #include &lt;iostream.h&gt;5:6:      int main()7:      {8:           int  intOne;9:           int &amp;rSomeRef = intOne;10:11:           intOne = 5;12:           cout &lt;&lt; &quot;intOne:\t&quot; &lt;&lt; intOne &lt;&lt; endl;13:           cout &lt;&lt; &quot;rSomeRef:\t&quot; &lt;&lt; rSomeRef &lt;&lt; endl;14:           cout &lt;&lt; &quot;&amp;intOne:\t&quot;  &lt;&lt; &amp;intOne &lt;&lt; endl;15:           cout &lt;&lt; &quot;&amp;rSomeRef:\t&quot; &lt;&lt; &amp;rSomeRef &lt;&lt; endl;16:17:           int intTwo = 8;18:           rSomeRef = intTwo;  // not what you think!19:           cout &lt;&lt; &quot;\nintOne:\t&quot; &lt;&lt; intOne &lt;&lt; endl;20:           cout &lt;&lt; &quot;intTwo:\t&quot; &lt;&lt; intTwo &lt;&lt; endl;21:           cout &lt;&lt; &quot;rSomeRef:\t&quot; &lt;&lt; rSomeRef &lt;&lt; endl;22:           cout &lt;&lt; &quot;&amp;intOne:\t&quot;  &lt;&lt; &amp;intOne &lt;&lt; endl;23:           cout &lt;&lt; &quot;&amp;intTwo:\t&quot;  &lt;&lt; &amp;intTwo &lt;&lt; endl;24:           cout &lt;&lt; &quot;&amp;rSomeRef:\t&quot; &lt;&lt; &amp;rSomeRef &lt;&lt; endl;25:      return 0;<TT>26: }</TT></FONT><FONT COLOR="#0066FF">Output: intOne:                 5rSomeRef:         5&amp;intOne:              0x213e&amp;rSomeRef:      0x213eintOne:                 8intTwo:                8rSomeRef:         8

⌨️ 快捷键说明

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