📄 1ptrref.html
字号:
<html>
<head>
<title>Pointers vs. References</title>
<meta name="description" content="Pointers and references in C++">
<meta name="keywords" content="pointer, reference, passing, variable">
<link rel="stylesheet" href="rs.css" tppabs="http://www.relisoft.com/book/rs.css">
</head>
<body background="margin.gif" tppabs="http://www.relisoft.com/book/images/margin.gif" bgcolor="#FFFFDC">
<!-- Main Table -->
<table cellpadding="6">
<tr>
<td width="78">
<td>
<h3>Pointers vs. References</h3>
<p>A pointer variable is declared with an asterisk between the type name and the variable name (the asterisk binds with the variable name). For instance,
<!-- Code -->
<table width="100%" cellspacing=10><tr>
<td class=codetable>
<pre>int *pValue;</pre>
</table>
<!-- End Code -->
declares <var>pValue</var> to be a pointer to an integer. The pointer can be initialized with (or assigned to, using the assignment operator = ) an address of some other variable. The <i>address of</i> operator is denoted by the ampersand (there is no conflict between this ampersand and the reference ampersand--they appear in different contexts). For instance,
<!-- Code --><table width="100%" cellspacing=10><tr> <td class=codetable>
<pre>int TheValue = 10;
pValue = &TheValue;</pre>
</table><!-- End Code -->
assigns the address of the variable <var>TheValue</var> to the pointer <var>pValue</var>.
<p><img src="Image1-2.gif" tppabs="http://www.relisoft.com/book/lang/pointer/images/Image1.gif" width=312 height=144 alt=" ">
<p class=caption>Figure 1. The pointer <var>pValue</var> stores the address of梡oints to梩he variable <var>TheValue</var>.</p>
<p>If we want to access the value to which the pointer points, we use the <i>dereference</i> operator, the asterisk (again, its double meaning doesn抰 lead to confusion). For instance,
<!-- Code --><table width="100%" cellspacing=10><tr> <td class=codetable><pre>int i = *pValue;
</pre></table><!-- End Code -->
assigns the value that <var>pValue</var> points to, to the integer <var>i</var>. In our example <var>i</var> will change its value to 10. Conversely,
<!-- Code --><table width="100%" cellspacing=10><tr> <td class=codetable>
<pre>*pValue = 20;</pre>
</table><!-- End Code -->
will change the value that <var>pValue</var> points to, that is, the value of <var>TheValue</var>. (The value of <var>TheValue</var> will now be 20). As you can see, this is a little bit more complicated than the use of references. By the way, the same example with references would look like this:
<!-- Code --><table width="100%" cellspacing=10><tr> <td class=codetable>
<pre>int TheValue = 10;
int& aliasValue = TheValue; // aliasValue refers to TheValue
int i = aliasValue; // access TheValue through an alias
aliasValue = 20; // change TheValue through an alias</pre>
</table><!-- End Code -->
<p>References definitely win the simplicity competition.
<p>Here抯 another silly example that shows how one could, but shouldn抰, use pointers. The only new piece of syntax here is the <b><i>member access</i></b> operator denoted by an arrow (actually a combination of a minus sign with a greater than sign) <var>-></var>. It combines dereferencing a pointer and accessing a member (calling a member function).
<!-- Code --><table width="100%" cellspacing=10><tr> <td class=codetable>
<pre>IStack TheStack;
IStack* pStack = &TheStack;
pStack->Push (7); // push 7 on TheStack using a pointer
// equivalent to (*pStack).Push (7)</pre>
</table><!-- End Code -->
<p>The same can be done using a reference:
<!-- Code --><table width="100%" cellspacing=10><tr> <td class=codetable>
<pre>IStack TheStack;
IStack& aliasStack = TheStack;
aliasStack.Push (7); // push 7 on TheStack using an alias</pre>
</table><!-- End Code -->
<p>Once more, references rule.
</table>
<!-- End Main Table -->
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -