C++ 默认情况下通过引用传递数组?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14312970/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Arrays passed by reference by default?
提问by johnbakers
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.
C++ 通过引用将数组传递给函数——被调用的函数可以修改调用者原始数组中的元素值。
It is referring to situations like this:
它指的是这样的情况:
int hourlyTemperatures[ 24 ];
modifyArray( hourlyTemperatures, 24 );
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?
但是,这是在这里工作的普通 C 数组指针,对吗?没有使用 C++“引用”技术,传递的是按值传递的指针,在这种情况下,是指向数组第一个元素的指针。最终结果是该函数确实可以访问完整的原始数组,就像引用一样,但这实际上不是按引用传递,对吗?
From this Prentice Hall book:
来自这本 Prentice Hall 的书:
采纳答案by Lightness Races in Orbit
You're right. The wording is very confusing and uses a meaning of "reference" that is not the same as the term referencerelating 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!
你是对的。措辞非常混乱,使用的“引用”含义与与同名C++ 特性相关的术语引用不同。相反,它讨论的是数组名称衰减为指针的方式——事实上,您根本不会像这样“传递数组”!
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++ doessupport things that it calls references; thus, we tend not to use "reference" in its "handle" sense when talking about C++ (where Deitel ? "we", evidently).
在“过去”中,“引用”以与“句柄”相同的方式在更一般的意义上使用 - 一个抽象术语,表示在不支持它的语言中使用间接来伪造按引用语义。但是,C++确实支持它称为引用的东西;因此,在谈论 C++ 时,我们倾向于不使用“句柄”意义上的“引用”(Deitel在哪里?“我们”,显然)。
Recommended reading:
推荐阅读:
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.
任何其他 C++ 书籍都要非常小心!!尽管在生活的大多数领域,如果我认为包含在上述两个特定列表中是一本书被认为是“好”的明确先决条件是疯狂的,但有足够丰富的危险不正确的 C++ 文本在那里(例如您引用的文本),这对于我们的语言新手来说是一个足够大的问题,在 C++ 书籍的世界中,它实际上是一个很好的经验法则。
回答by bcfehrman
It is true that when you pass an array to a function that you are actually passing a pointer by value. However, I feel that for people just learning C++ they should not have to worry about pointers at all. They learn that you can pass a variable by value or by reference to a function.
确实,当您将数组传递给函数时,您实际上是在按值传递指针。但是,我觉得对于刚刚学习 C++ 的人来说,他们根本不必担心指针。他们了解到您可以通过值或通过引用函数来传递变量。
By value means that changes to the variable in the function don't affect the original value in the calling function.
通过值意味着对函数中变量的更改不会影响调用函数中的原始值。
By reference means that if the function changes the value of the variable then those changes will be seen in the original calling function.
引用意味着如果函数更改了变量的值,那么这些更改将在原始调用函数中看到。
When an array is a parameter of a function (without the const keyword) then any changes made to the values in the array will be seen in the original calling function. Therefore, we say that arrays are passed by reference by default. This avoids having to explain what pointers are which really isn't that relevant in passing statically declared arrays around, yet it lets people know that if they mess with the values in the array in the called function that those changes will persist in the calling function.
当数组是函数的参数(没有 const 关键字)时,对数组中的值所做的任何更改都将在原始调用函数中看到。因此,我们说数组默认是通过引用传递的。这避免了必须解释哪些指针实际上与传递静态声明的数组无关,但它让人们知道,如果他们在被调用函数中混淆了数组中的值,这些更改将在调用函数中持续存在.
I teach a "first-course" Computer Science C++ course at a top-notch engineering school (our programming team is going to the World Finals in Russia this year). About 90% of the people in the class aren't actually computer related majors (mostly mechanical engineers). The above material is more than enough to confuse them without having to explain what pointers are. That is why that book and others mention that arrays are passed by reference because many people reading the books just need "just enough" C++ to get them by without having to learn every little detail. Those that really want to program should have no problem transitioning to fact that arrays are really passed by pointer.
我在一流的工程学院教授“第一门课”计算机科学 C++ 课程(我们的编程团队今年将参加俄罗斯的世界总决赛)。班上大约 90% 的人实际上不是计算机相关专业(主要是机械工程师)。上面的材料足以混淆它们而无需解释什么是指针。这就是为什么那本书和其他人提到数组是通过引用传递的,因为许多阅读这些书的人只需要“刚好”的 C++ 来获得它们,而不必学习每一个细节。那些真正想要编程的人应该可以毫无问题地过渡到数组实际上是通过指针传递的事实。
Just my opinion though.
只是我的意见。
回答by Karthik T
To summarise my comment, you are absolutely right!
总结一下我的评论,你是绝对正确的!
The book is wrongin its choice of jargon. It tries to talk about arrays decaying to C pointers. It refers to passing this pointer by valueas passing by referencewhich is WRONG.
这本书在术语的选择上是错误的。它试图谈论衰减到 C 指针的数组。它指的是按值传递这个指针作为按引用传递是错误的。
This is a common enough misconception, I believe I was taught this way as well (passing pointer as value == passing by reference). This is completely wrong in terms of C++ references and pass by reference.
这是一个很常见的误解,我相信我也是这样教的(将指针作为值传递 == 通过引用传递)。这在 C++ 引用和按引用传递方面是完全错误的。
If this were correct, I wouldn't be able to do this..
如果这是正确的,我将无法做到这一点..
void ModifyMyArray(int *array){
int oops[4]= {0};
array = oops;
array[2] = 1;
}
...
int MyArray[4] = {1,3,5,7};
ModifyMyArray(MyArray);
Similar to this question in Java - Is Java "pass-by-reference" or "pass-by-value"?
类似于 Java 中的这个问题 - Java是“pass-by-reference”还是“pass-by-value”?