📄 passby.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>参数的传值、传参考</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: 参数传递的传值、传参考</a></h1>
C++在呼叫函式时的参数传递方式主要有两种:传值(Pass by value)、传参考(Pass by
reference)。 <br>
<br>
参数传递时的传值就是传送(变数)值给函式上对应的参数,值被复制一份给参数,传递者与接受者两个变数彼此各占有一个记忆体,互不相干,例如: <br>
<div style="margin-left: 40px;"><span style="font-family: Courier New,Courier,monospace; font-weight: bold;">int
main() { </span><br style="font-family: Courier New,Courier,monospace; font-weight: bold;">
<span style="font-family: Courier New,Courier,monospace; font-weight: bold;">
int x = 10; </span><br style="font-family: Courier New,Courier,monospace; font-weight: bold;">
<span style="font-family: Courier New,Courier,monospace; font-weight: bold;">
.... </span><br style="font-family: Courier New,Courier,monospace; font-weight: bold;">
<span style="font-family: Courier New,Courier,monospace; font-weight: bold;">
cout << increment(x)
<< endl; </span><br style="font-family: Courier New,Courier,monospace; font-weight: bold;">
<span style="font-family: Courier New,Courier,monospace; font-weight: bold;">
cout << x << endl; </span><br style="font-family: Courier New,Courier,monospace; font-weight: bold;">
<br style="font-family: Courier New,Courier,monospace; font-weight: bold;">
<span style="font-family: Courier New,Courier,monospace; font-weight: bold;">
return 0; </span><br style="font-family: Courier New,Courier,monospace; font-weight: bold;">
<span style="font-family: Courier New,Courier,monospace; font-weight: bold;">
} </span><br style="font-family: Courier New,Courier,monospace; font-weight: bold;">
<br style="font-family: Courier New,Courier,monospace; font-weight: bold;">
<span style="font-family: Courier New,Courier,monospace; font-weight: bold;">
int increment(int n) { </span><br style="font-family: Courier New,Courier,monospace; font-weight: bold;">
<span style="font-family: Courier New,Courier,monospace; font-weight: bold;">
n = n + 1; </span><br style="font-family: Courier New,Courier,monospace; font-weight: bold;">
<span style="font-family: Courier New,Courier,monospace; font-weight: bold;">
return n; </span><br style="font-family: Courier New,Courier,monospace; font-weight: bold;">
<span style="font-family: Courier New,Courier,monospace; font-weight: bold;">
}</span><br>
</div>
<span style="font-style: italic;"><span style="font-weight: bold;"><br>
</span></span>
在这个程式片段中,x将值传递给increment()函式的参数n,n虽然作了递增运算,但是对x的储存值并无影响,x最后仍是显示10。 <br>
<br>
在传值应用上,您也可以将变数的记忆体位址值取出,传递给指定的指标参数,只要使用&运算子就可以了。<br>
<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">int main() { </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">
int
x = 10; </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">
....
</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">
cout
<< increment(&x) << endl; </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">
cout
<< x << endl; </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">
return 0; </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">} </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">int
increment(int
*n) { </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">
*n =
*n + 1; </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">
return *n; </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">}</span><br>
</div>
<br>
在这个程式中,increment()上的参数n是个指标,在呼叫increment()函式时,您使用取址运算子&将x变数的记忆体位
址取出并
传递给指标n,而在函式中,您使用取值运算子*取出这块记忆体位址的值,并作递增动作之后再指定回该记忆体位址,所以程式最后的x变数会显示值11。 <br>
<br>
在函式上宣告指标参数的目的,通常目的是若作为引数的变数值同一位址上,在函式中若有变动该位址上的值时,呼叫者也可以保留这份变动的结果,这可以解决一
个问题,在C++中我们在呼叫函式后只能传回
(return)一个值,若在呼叫函式时,您希望能取得两个以上的运算结果,就可以使用指标参数。<br>
<br>
在 <a href="Reference.html">参考(Reference)</a>
中介绍过参考型态,参考型态最常使用于函式的参数列上,一个使用的例子如下:<br>
<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">int main() { </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">
int
x = 10; </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">
....
</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">
cout
<< increment(x) << "\n"; </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">
cout
<< x << "\n"; </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">
return 0; </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">} </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">int
increment(int
&n) { </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">
n =
n + 1; </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">
return n; </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">}</span><br>
</div>
<br>
在上面的程式片段中,您在increment()的参数列中使用参考宣告int&,当呼叫increment()函式时,您不用使用
&运
算子取得x变数的记忆体位址,n会自动参考至x变数的位址,由于x变数与n变数的记忆体位址是相同的,所以在increment()中的递增动作,同样会
影响x变数,所以程式最后x会显示为11。 <br>
<br>
传参考使用的时机使在于您希望传递的参数,在函式中若有变动时,呼叫者也可以保留这份变动的结果,所以若在呼叫函式时,您希望能取得两个以上的运算结果,
就可以使用传址呼叫,另一个使用的时机则是在大型物件的传递,如果使用传值呼叫,大型物件在复制时会占用一块大的记忆体,此时您可以使用传参考,如果您不
想在函式中改变物件的话,而只想让物件提供资讯,则可以加上const修饰,例如:<br>
<div style="margin-left: 40px; font-weight: bold; font-family: Courier New,Courier,monospace;">void
someFunction(const SomeType &some) {<br>
....<br>
}
</div>
<br>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -