作者march20 ()
看板Programming
標題Re: 請問call by referance !!
時間Mon Oct 2 16:21:05 2006
※ 引述《costbook (antitheist)》之銘言:
: 以下是我在學校網站,和某人討論的結果...
: 我:
: 原來C語言沒有call by address啊
: 某人:
: c語言有2種參數:
: 1. call by value→傳值
: 2. call by address→傳址
: 第2個,其泛用性定義:call by address = call by reference
: 我:
: reference不是C++的東西嗎?
: 某人:
: 找幾本有關C/C++的原文電子書,然後再用關鍵字去
: 搜尋"Call by Address"和"Call by Refrence",看
: 外國做這是如何給予定義。
查了一下 wikipedia 覺得以下這段話滿合理的:
(出自
http://en.wikipedia.org/wiki/Evaluation_strategy )
In call-by-reference evaluation, a function is passed an implicit
reference to its argument rather than the argument value itself.
If the function is able to modify such a parameter, then any changes
it makes will be visible to the caller as well. If the argument
expression is an L-value, its address is used. Otherwise, a temporary
object is constructed by the caller and a reference to this object is
passed; the object is then discarded when the function returns.
Some languages contain a notion of references as first-class values.
ML, for example, has the "ref" constructor; references in C++ may also
be created explicitly. In these languages, "call-by-reference" may be
used to mean passing a reference value as an argument to a function.
In languages (such as C) that contain unrestricted pointers instead
of or in addition to references, call-by-address is a variant of
call-by-reference where the reference is an unrestricted pointer.
就看你怎樣看待 pointer 和 reference 囉.
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 71.136.250.102
※ 編輯: march20 來自: 71.136.250.102 (10/02 16:22)
※ 編輯: march20 來自: 71.136.250.102 (10/02 17:28)
→ march20:我覺得最混淆的地方來自 = 這個 operator 71.136.250.102 10/02 17:34
→ march20:C++ 把 = 當 copy 和 assignment 來用 71.136.250.102 10/02 17:35
→ march20:遇到 reference 時,=operator變得很不自然 71.136.250.102 10/02 17:35
推 costbook:真複雜啊... 61.217.132.5 10/02 21:03