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

📄 reference.html

📁 关于 C++ 的历史无须我来介绍了
💻 HTML
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>

  
  <link rel="stylesheet" href="css/stdlayout.css" type="text/css">

  
  <link rel="stylesheet" href="css/print.css" type="text/css">

  
  <meta content="text/html; charset=gb2312" http-equiv="content-type">

  
  <title>参考(Reference)</title>
</head>


<body>

<h3><a href="http://caterpillar.onlyfun.net/GossipCN/index.html">From
Gossip@caterpillar</a></h3>

<h1><a href="CppGossip.html">C++
Gossip:&nbsp;参考(Reference)</a></h1>

参考(Reference)型态代表了变数或物件的一个别名(Alias),参考型态可以直接取得变数或物件的位址,并间接透过参考型态别名来操作物件,
作用类似于指标,但却不必使用指标语法,也就是不必使用*运算子来提取值。<br>

<br>

要定义参考型态,在定义型态时于型态关键字后加上&amp;运算子,例如:<br>

<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">int var = 10; &nbsp;//
定义变数</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">int *ptr =
&amp;var; // 定义指标,指向var的位址</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">int &amp;ref =
var; &nbsp;// 定义参考,代表var变数</span><br>

</div>

<br>

上面的程式中,最后一行即是在定义参考型态,注意参考型态一定要初始化,例如下面的定义是不能通过编译的:<br>

<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">int &amp;ref; // error,
`ref' declared as reference but not initialized </span><br>

</div>

<br>

为何参考型态一定要初始化?因为参考初始化后就不能改变它所代表的物件,任何指定给参考的值,就相当于指定给原来的物件,例如:<br>

<br>

<pre>#include &lt;iostream&gt;<br>using namespace std;<br><br>int main() {<br>    int var = 10;<br>    int &amp;ref = var;<br>    <br>    cout &lt;&lt; "var: " &lt;&lt; var <br>         &lt;&lt; endl;<br>    cout &lt;&lt; "ref: " &lt;&lt; ref<br>         &lt;&lt; endl;<br>         <br>    ref = 20;<br><br>    cout &lt;&lt; "var: " &lt;&lt; var <br>         &lt;&lt; endl;<br>    cout &lt;&lt; "ref: " &lt;&lt; ref<br>         &lt;&lt; endl;<br><br>    return 0;<br>}<br></pre>

<br>

<span class="postbody">
执行结果:</span><br>

<table style="text-align: left; width: 100%;" border="0" cellpadding="2" cellspacing="2">

  <tbody>

    <tr>

      <td style="background-color: rgb(0, 0, 0);"><small><span style="color: rgb(255, 255, 255);">var: 10<br>

ref: 10<br>

var: 20<br>

ref: 20</span></small><span style="color: rgb(255, 255, 255);"><br>

      </span></td>

    </tr>

  
  </tbody>
</table>

<br>

您也可以参考至一个字面常量,例如:<br>

<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">const int &amp;ref = 10;</span><br>

</div>

<br>

为什么要在前面加上const才能参考至一个字面常量呢?您知道字面常量是不可定址的,为了能够让符合参考定址的语义,上面这段程式编译器会传如下的转
换:<br>

<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">int tmp = 10;</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">const int &amp;ref
= tmp;</span><br>

</div>

<br>

先想想没有加上const的情况,如果您对ref重新指定值,则实际改变的是tmp的值,而不是字面常量10,这就在符合字面常量无法取址(也就无法改变
位址上的值)的语义,但使用者可能困惑明明改变了ref,为何字面常量没有改变,所以加上const,明确指示不可以再重新指定值给ref,例如:<br>

<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">const int &amp;ref = 10;</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">ref = 20; //
error, assignment of read-only reference `ref' </span><br>

</div>

<br>

如果要定义指标型态的参考该如何呢?很简单,指标型态是使用type*来宣告,而参考则是在名称前加上&amp;,所以指标型态的参考就如下所示:<br>

<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">type *&amp;refOfPtr =
somePtr;</span><br>

</div>

<br>

一个具体的例子如下:<br>

<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">int var = 10;</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">int *ptr =
&amp;var;</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">int *&amp;ref =
ptr;</span><br>

</div>

<br>

举一反三的话,如果有个const变数,您可以使用一个const指标,并可以如下宣告一个指标的参考:<br>

<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">const int var = 10;</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">const int *ptr =
&amp;var;</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">const int
*&amp;ref = ptr;</span><br>

</div>

<br>

事实上很少会直接如上的方式来使用参考,而是用于函式传递时一种“传参考”(Pass by
reference)方式,目的在于可于函式中直接操作目标变数或物件,或者是避免复制一个大型物件,在之后要绍函式时会见到相关应用。<br>

<br>

<br>

</body>
</html>

⌨️ 快捷键说明

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