通过引用传递的指针每次循环迭代更新:(Sanity检查 - 第2部分) [英] Pointers passed by reference updated with every loop iteration: (Sanity check - Part 2)
问题描述
我试图把握与指针和引用相关的概念。在下面的代码片段中,我从迭代链接列表时从for循环调用function()多次。每次迭代, int * ptr 和 int count 都会更改并传递给函数shif()。此外,还有一些其他指针和整数变量会通过引用传递。
I am trying to grasp come concepts related to pointers and references. In the code snippet below, I call function shifting() several times from a for loop while iterating a linked list. With each iteration, int *ptr and int count are changed and passed to the function shifting(). Also, some other pointers and integer variables are passed by reference.
怀疑:
-
我可以分配指针引用,如shif()函数中所示?
can I assign pointer references like shown in shifting() function? Same question for integer val references.
背景中发生了什么?我读的引用不能重新分配。这是不是这里每次shift()被调用?
What is going on in the background? I read that references cannot be re-assigned. Is this not the case here every time shifting() is called?
请注意,* ptr和计数不通过引用传递。
please note, *ptr and count are NOT passed by reference. They are only to be read.
void shifting(int const * ptr, int const * &ptr6, int const * &ptr7, int const * &ptr8,
int count, int &val6, int &val7, int &val8)
{
ptr8 = ptr7; val8 = val7;
ptr7 = ptr6; val7 = val6;
ptr6 = ptr; val6 = count;
}
int main()
{
int const *ptr6 = NULL; int val6 = 0;
int const *ptr7 = NULL; int val7 = 0;
int const *ptr8 = NULL; int val8 = 0;
int count = 0;
// myList: a linked-list
// front(): gives first element of list
// back(): gives last element of list
// nextElement(): gives next element of list
for (int *ptr = myList.front(); ptr != myList.back(); ptr = nextElement();)
{
count++;
shifting(ptr, ptr6, ptr7, ptr8, count, val6, val7, val8);
}
}
EDIT:上面的例子(这里张贴后)只有整数部分,如下所示:
I tried the above example (after posting here) with only integer part as shown below:
#include <iostream>
using namespace std;
void shifting( int i, int &val6, int &val7, int &val8 )
{
val8 = val7;
val7 = val6;
val6 = i;
}
int main()
{
int val6 = 0;
int val7 = 0;
int val8 = 0;
for (int i = 1; i <= 10; i++)
{
shifting(i, val6, val7, val8);
cout <<"i: "<<i<<" val6: "<<val6<<" val7: "<<val7<<" val8: "<<val8<<endl;
}
return 0;
}
我得到这个输出如下。如何引用被重新分配???我读了他们不应该重新分配。
I got this output as below. How come references are being re-assigned??? I read they are not supposed to reassign.
i: 1 val6: 1 val7: 0 val8: 0
i: 2 val6: 2 val7: 1 val8: 0
i: 3 val6: 3 val7: 2 val8: 1
i: 4 val6: 4 val7: 3 val8: 2
i: 5 val6: 5 val7: 4 val8: 3
i: 6 val6: 6 val7: 5 val8: 4
i: 7 val6: 7 val7: 6 val8: 5
i: 8 val6: 8 val7: 7 val8: 6
i: 9 val6: 9 val7: 8 val8: 7
i: 10 val6: 10 val7: 9 val8: 8
推荐答案
我认为你可能会通过在函数和调用者之间重用变量名来部分混淆自己:例如,在移动 val6
是一个局部变量,绑定到它调用的任何内容,只是恰好是一个具有相同名称的外部变量。
I think you may partly be confusing yourself by re-using variable names between the function and the caller: For example, within shifting val6
is a local variable, bound to whatever it was called with, which just happens to be an external variable with the same name.
#include <iostream>
void test(int& i) {
std::cout << "test/i = " << i << '\n';
}
int main() {
int i = 1;
int j = 42;
test(i);
test(j);
}
此输出(请参阅 http://ideone.com/lRNINP )
test/i = 1
test/i = 42
作为本地引用的意义是
我得到这个输出如下所示。
I got this output as below. How come references are being re-assigned???
它们不是,值只是移动,因为你的函数出现的目的做:(请参阅 http://ideone.com/oCmOPk )
They aren't, the values are simply shifting as your function appears to aim to do: (see http://ideone.com/oCmOPk)
#include <iostream>
int main() {
int i = 0;
int& ir = i; // not an assignment, a binding
ir = 1; // pass '1' thru 'ir' to 'i'.
std::cout << "ir " << ir << ", i " << i << "\n";
int j = 0;
int& jr = j;
jr = 42; // pass '42' thru 'jr' to 'j'
std::cout << "jr " << jr << ", j " << j << "\n";
ir = jr; // pass the *value* of 'jr' thru 'ir' to 'i'
j = 99;
// if ir is rebound to jr, both of these will be 99.
std::cout << "ir " << ir << ", i " << i << "\n";
std::cout << "jr " << jr << ", j " << j << "\n";
}
输出:
ir 1, i 1
jr 42, j 42
ir 42, i 42
jr 99, j 99
这篇关于通过引用传递的指针每次循环迭代更新:(Sanity检查 - 第2部分)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!