?? 009.htm
字號:
<HTML><HEAD><meta http-equiv="Content-Type" content="text/html; charset=GB2312"><TITLE>-->Learn C++ in 21 Days-->Day 9</TITLE>
<META NAME="888" CONTENT=" Learn C++ in 21 Days Day 9">
<META NAME="888" CONTENT=" - Learn C++ in 21 Days - Day 9">
<style>
<!--
#page {position:absolute; z-index:0; left:0px; top:0px}
.tt3 {font: 9pt/12pt "宋體"}
.tt2 {font: 12pt/15pt "宋體"}
a {text-decoration:none}
a:hover {color: blue;text-decoration:underline}
-->
</style>
</HEAD>
<p align="center"><script src="../../../1.js"></script> </p>
<TD CLASS="tt3" VALIGN="top" width="8%" bgcolor="#ffffff"><strong><A HREF="010.htm"><FONT style="FONT-SIZE: 9pt">后一頁</font></A><BR>
<A HREF="008.htm"><FONT style="FONT-SIZE: 9pt">前一頁</font></A><BR>
<A HREF="index.html"><FONT style="FONT-SIZE: 9pt">回目錄</font></A><BR>
<TD class="tt2" bgcolor="#ffffff" width="100%"><center><B><FONT style="FONT-SIZE: 16.5pt" COLOR="#FF6666" FACE="楷體_GB2312">Day 9</FONT></B></center>
<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 & 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&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>
<BR>
<P>
<HR>
<BR>
<BR>
<H2 ALIGN="CENTER"><A NAME="Heading1"></A><FONT COLOR="#000077">Day 9</FONT></H2>
<BR>
<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 store
and 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 you
learn the following
<BR>
<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>
<BR>
<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 the
name of another object, the target. From that moment on, the reference acts as an
alternative name for the target, and anything you do to the reference is really done
to the target.</P>
<P>You create a reference by writing the type of the target object, followed by the
reference operator (<TT>&</TT>), followed by the name of the reference. References
can use any legal variable name, but for this book we'll prefix all reference names
with "r." 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" font style="font-size:10pt">int &rSomeRef = someInt;
</FONT></PRE>
<P>This is read as "<TT>rSomeRef</TT> is a reference to an integer that is initialized
to refer to <TT>someInt</TT>." Listing 9.1 shows how references are created
and used.
<BR>
<BR>
<BLOCKQUOTE>
<P>
<HR>
<FONT COLOR="#000077"><B>NOTE:</B></FONT><FONT COLOR="#0000AA"><B> </B></FONT>Note
that the reference operator (<TT>&</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>
<BR>
<BR>
</BLOCKQUOTE>
<BR>
<P><A NAME="Heading4"></A><FONT SIZE="4" COLOR="#000077"><B>Listing 9.1. Creating
and using references.</B></FONT>
<PRE><FONT COLOR="#0066FF" font style="font-size:10pt">
1: //Listing 9.1
2: // Demonstrating the use of References
3:
4: #include <iostream.h>
5:
6: int main()
7: {
8: int intOne;
9: int &rSomeRef = intOne;
10:
11: intOne = 5;
12: cout << "intOne: " << intOne << endl;
13: cout << "rSomeRef: " << rSomeRef << endl;
14:
15: rSomeRef = 7;
16: cout << "intOne: " << intOne << endl;
17: cout << "rSomeRef: " << rSomeRef << endl;
18: return 0;
<TT>19: }</TT></FONT>
<FONT COLOR="#0066FF">
Output: intOne: 5
rSomeRef: 5
intOne: 7
rSomeRef: 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 you
declare 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 Address
of Operator & 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 demonstrates
this.</P>
<BR>
<P><A NAME="Heading6"></A><FONT SIZE="4" COLOR="#000077"><B>Listing 9.2. Taking the
address of a reference</B></FONT><FONT SIZE="2" COLOR="#000077"><B>.</B></FONT>
<PRE><FONT COLOR="#0066FF" font style="font-size:10pt">
1: //Listing 9.2
2: // Demonstrating the use of References
3:
4: #include <iostream.h>
5:
6: int main()
7: {
8: int intOne;
9: int &rSomeRef = intOne;
10:
11: intOne = 5;
12: cout << "intOne: " << intOne << endl;
13: cout << "rSomeRef: " << rSomeRef << endl;
14:
15: cout << "&intOne: " << &intOne << endl;
16: cout << "&rSomeRef: " << &rSomeRef << endl;
17:
18: return 0;
<TT>19: }</TT></FONT>
<FONT COLOR="#0066FF">
Output: intOne: 5
rSomeRef: 5
&intOne: 0x3500
&rSomeRef: 0x3500
</FONT></PRE>
<BR>
<BR>
<BLOCKQUOTE>
<P>
<HR>
<FONT COLOR="#000077"><B>NOTE:</B></FONT><B> </B>Your output may differ on the last
two lines.
<HR>
<BR>
<BR>
</BLOCKQUOTE>
<BR>
<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 the
addresses of the two variables are printed, and they are identical. C++ gives you
no 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 initialized
when created, and always act as a synonym for their target, even when the address
of operator is applied.<BR>
For example, if you have a class called <TT>President</TT>, you might declare an
instance of that class as follows:</P>
<PRE><FONT COLOR="#0066FF" font style="font-size:10pt">President William_Jefferson_Clinton;</FONT></PRE>
<P>You might then declare a reference to <TT>President</TT> and initialize it with
this object:</P>
<PRE><FONT COLOR="#0066FF" font style="font-size:10pt">President &Bill_Clinton = William_Jefferson_Clinton;</FONT></PRE>
<P>There is only one <TT>President</TT>; both identifiers refer to the same object
of 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>&</TT> symbol on line 9 of Listing
9.2, which declares a reference to <TT>int</TT> named <TT>rSomeRef</TT>, and the
<TT>&</TT> symbols on lines 15 and 16, which return the addresses of the integer
variable <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. You
simply use the reference as you would use the target variable. This is shown on line
13.</P>
<P>Even experienced C++ programmers, who know the rule that references cannot be
reassigned and are always aliases for their target, can be confused by what happens
when you try to reassign a reference. What appears to be a reassignment turns out
to be the assignment of a new value to the target. Listing 9.3 illustrates this fact.</P>
<BR>
<P><A NAME="Heading8"></A><FONT SIZE="4" COLOR="#000077"><B>Listing 9.3. Assigning
to a reference.</B></FONT>
<PRE><FONT COLOR="#0066FF" font style="font-size:10pt">
1: //Listing 9.3
2: //Reassigning a reference
3:
4: #include <iostream.h>
5:
6: int main()
7: {
8: int intOne;
9: int &rSomeRef = intOne;
10:
11: intOne = 5;
12: cout << "intOne:\t" << intOne << endl;
13: cout << "rSomeRef:\t" << rSomeRef << endl;
14: cout << "&intOne:\t" << &intOne << endl;
15: cout << "&rSomeRef:\t" << &rSomeRef << endl;
16:
17: int intTwo = 8;
18: rSomeRef = intTwo; // not what you think!
19: cout << "\nintOne:\t" << intOne << endl;
20: cout << "intTwo:\t" << intTwo << endl;
21: cout << "rSomeRef:\t" << rSomeRef << endl;
22: cout << "&intOne:\t" << &intOne << endl;
23: cout << "&intTwo:\t" << &intTwo << endl;
24: cout << "&rSomeRef:\t" << &rSomeRef << endl;
25: return 0;
<TT>26: }</TT></FONT>
<FONT COLOR="#0066FF">
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -