📄 right2-5.htm
字号:
<html>
<head>
<meta http-equiv="Content-Language" content="zh-cn">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>第 2 章</title>
<base target="rbottom">
<style>
<!--
.右标题 { font-size: 10pt; color: #000080; text-indent: 0; margin: 0 }
.右内容 { font-size: 10pt; text-align: left; text-indent: 0; line-height: 100%; margin:
0 }
-->
</style>
</head>
<body>
<p style="line-height: 100%; margin-top: 0; margin-bottom: 0"> </p>
<p style="line-height: 100%; margin-top: 0; margin-bottom: 0"><span style="position: absolute; left: 4; top: -8"><img border="0" src="1.gif" width="63" height="70"></span></p>
<p style="line-height: 100%; margin-top: 0; margin-bottom: 0"> </p>
<p style="line-height: 100%; text-indent: 0; margin: 0" class="右标题" align="left">
<span style="background-color: #CCFF99">第 2 章</span><span style="background-color: #CCFF99">
</span>><span style="background-color: #CCFF99"> 第 5 节 </span>><span style="background-color: #CCFF99">
2.5 引用</span></p>
<hr color="#008000" size="1">
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font LANG="ZH-CN" size="2">引用就是为某个变量或隐含的临时变量起个别名,对别名的操作等同于对目标变量的操作。</font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font LANG="ZH-CN" size="2">1.
引用的声明</font></p>
<font SIZE="3">
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"></font><font LANG="ZH-CN" size="2">引用的声明形式为:</font></p>
<blockquote>
<blockquote>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2" color="#008000"><font LANG="ZH-CN">类型</font>&
<font LANG="ZH-CN">变量</font>=<font LANG="ZH-CN">目标对象</font>;</font></p>
</blockquote>
</blockquote>
<font SIZE="3">
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"></font><font size="2"><font LANG="ZH-CN">其中的变量就是目标对象的引用。引用的类型为“<font color="#008000">类型</font></font><font color="#008000">&</font><font LANG="ZH-CN">”。</font></font></p>
<font SIZE="3">
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"></font><font LANG="ZH-CN" size="2">例如:</font></p>
<blockquote>
<blockquote>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">int
sum, *p;</font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">int&
rsum=sum;</font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">int&
rcount=0;</font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">int*&
rp=p;</font></p>
</blockquote>
</blockquote>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2"><font LANG="ZH-CN">这里,</font>rsum<font LANG="ZH-CN">是变量</font>sum<font LANG="ZH-CN">的引用,类型为</font>int&<font LANG="ZH-CN">;</font>rcount<font LANG="ZH-CN">是一个内部临时变量的引用,类型为</font>int&<font LANG="ZH-CN">,该临时变量并不显式的表示出来。</font><font color="#FF6600">rp<font LANG="ZH-CN">是</font>*p<font LANG="ZH-CN">的引用,类型为</font>int*&<font LANG="ZH-CN">。</font></font></font></p>
<font SIZE="3">
<p ALIGN="justify" style="line-height: 100%; margin-top: 10; margin-bottom: 0"></font><font LANG="ZH-CN" size="2">说明:</font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font LANG="ZH-CN" size="2">①
引用本身不是变量,它是某个对象的别名,其本身不占存储空间。</font></p>
<font SIZE="3">
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"></font><font LANG="ZH-CN" size="2">②
定义引用时必须指出目标对象,目标对象必须是单个对象且已定义或已声明。</font></p>
<font SIZE="3">
<p ALIGN="justify" style="line-height: 100%; margin-top: 10; margin-bottom: 0"></font><font LANG="ZH-CN" size="2">引用的特点为:</font></p>
<font SIZE="3">
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"></font><font LANG="ZH-CN" size="2">①
引用声明后,对引用的操作等同于对目标对象的操作。</font></p>
<font SIZE="3">
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"></font><font LANG="ZH-CN" size="2">②
引用一旦初始化,它就维系在一个固定的目标上,再也不分开。</font></p>
<p ALIGN="justify" style="line-height: 200%; margin-top: 0; margin-bottom: 0"><font LANG="ZH-CN" size="2">2.
引用的操作</font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">(1)
<font LANG="ZH-CN">用引用传递函数参数</font></font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2"><font LANG="ZH-CN">【例</font>2-15<font LANG="ZH-CN">】用“引用”作函数的参数。</font></font></p>
<blockquote>
<blockquote>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">#include
"iostream.h"</font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">void
swap(int &x,int &y)</font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">{
int j;</font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">
j=x;</font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">
x=y;</font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">
y=j;</font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">}</font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">void
main( )</font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">{
long a=12345,b=54321;</font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">
cout<<a<<" "<<b<<endl;</font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">
swap(a,b);</font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">
cout<<a<<" "<<b<<endl;}</font></p>
</blockquote>
</blockquote>
<font SIZE="3">
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -