阵列由默认引用传递? [英] Arrays passed by reference by default?

查看:132
本文介绍了阵列由默认引用传递?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读一个C ++的书,这样说:

I'm reading a C++ book which says this:

的C ++通过引用被调用函数传递阵列功能可以在呼叫者的原始阵列修改元素值

C++ passes arrays to functions by reference—the called functions can modify the element values in the callers’ original arrays.

这是指的情况是这样的:

It is referring to situations like this:

int hourlyTemperatures[ 24 ]; 
modifyArray( hourlyTemperatures, 24 );

不过,这是香草数组c的指针在这里工作,对不对?没有使用一个C ++参考​​的技术,什么被传递是一个的指针由值的,在这种情况下,一个指针数组的第一个元素。最终的结果是,该函数可以访问完整的原始阵列,像一个参考,但实际上不是按引用传递,对吧?

However, this is vanilla C array pointers at work here, right? There is no use of a C++ "reference" technique, what is being passed is a pointer by value, in this case, a pointer to the first element of the array. The end result is that the function does have access to the full original array, like a reference, but this is not actually pass by reference, right?

从这个prentice堂书:

From this Prentice Hall book:

推荐答案

您说得对。措辞非常混乱,并且使用的引用,也就是不一样的人对相同名字的C ++功能术语的参考的意义。相反,它在谈论的方式,数组名衰变为指针—其实你根本不传递一个数组像这样!

You're right. The wording is very confusing and uses a meaning of "reference" that is not the same as the term reference relating to the C++ feature of the same name. Instead, it's talking about the way that array names decay to pointers — in fact you do not "pass an array" like this at all!

在古时,参考是在一个更一般的意义上使用以相同的方式处理—一个抽象的术语来重新present使用间接假by-reference语义在不支持它的语言。但是,C ++的确实的支持的东西,它调用的引用的;因此,我们往往不会在其处理意义上讨论关于C时使用的引用++(其中的的Deitel∉我们的,看样子)。

In the "olden days" "reference" was used in a more general sense in the same way as "handle" — an abstract term to represent the use of indirection to fake by-reference semantics in languages that did not support it. But, C++ does support things that it calls references; thus, we tend not to use "reference" in its "handle" sense when talking about C++ (where Deitel ∉ "we", evidently).

推荐阅读:

  • http://jcatki.no-ip.org/fncpp/Resources
  • The Definitive C++ Book Guide and List

任何其他C ++的书的非常谨慎的!虽然在大多数生活领域那将是疯狂的我的建议,列入上述两项具体名单是一个明确的pre-必要的一本书被认为是好,有一种危险有足够的财富不正确C ++的文字有(比如你引用的文字),这是我们的语言新人足够大的问题,在C ++的书的世界它实际上是拇指遵循的一个好的规则。

Any other C++ book be very wary!! Though in the majority of areas of life it would be insane of me to suggest that inclusion in the above two specific lists is a definitive pre-requisite for a book to be considered "good", there is a sufficient wealth of dangerously incorrect C++ text out there (such as the text you quote) and this is a sufficiently big problem for our language newcomers that in the world of C++ books it's actually a good rule of thumb to follow.

这篇关于阵列由默认引用传递?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆