简体   繁体   English

有人可以解释一下这段代码是如何执行的吗?

[英]Can someone explain me how exactly this code is executed?

Can someone explain how the code is executed in here? 有人可以在这里解释代码的执行方式吗? I don't understand how the output in the second line is 8 7 8 and in the third line is 21 20 21. 我不明白第二行的输出是8 7 8,第三行的输出是21 20 21。

#include<iostream>
using namespace std;
//Funtion passed by value and by reference
int fn1(int &a, int b, int *c){
   a=b++;
   b+=7;
   *c= ++a;

   return b;
}

int main(){
   int x=4, y=7, z=14;
   cout<< x<< " " << y<< " "<< z<< endl; // output: 4 7 14
   fn1(x,y,&z);
   cout<< x<< " " << y<< " "<< z<< endl; // output: 8 7 8 (I dont get this part!!!)
   x=9, y=12, z=19;

   y=fn1(x,y,&z);
   fn1(x,y,&z);
   cout<< x<< " " << y<< " "<< z<< endl; // output: 21 20 21(I dont get this part!!!)
   return 0;
}  

You are creating a reference for a passing b as it is and c's address 您正在为传递的b和c的地址创建引用

a is a reference to x hence any change in value of a is reflected in x
since y is passed by value any change in value of b will not change y
and for z as its address is passed any change to the location will be reflected in z

for a = b++
a gets the value 7 and b is incremented to 8(post increment)
b+=7
*c = ++a
a will become 8 and will get assigned to address pointed by c
hence you get output as 8 7 8 as x will be a y will remain 7 and z will be c

same for the next two calls 接下来的两个电话相同

Second line: 第二行:

x is modified by fn1 because the first parameter is passed by reference. xfn1修改,因为第一个参数是通过引用传递的。

z is modified by fn1 because a pointer to z is passed to the function. fn1修改了z ,因为将指向z的指针传递给该函数。

The first line: 第一行:

a = b++

Assigns 7 to a because b is 7 and b++ is a post-increment function. a分配为7 ,因为b7b++是后递增函数。

c = ++a

Increments a to 8 and assign 8 to c because ++a is a pre-increment function. 因为++a是一个预递增函数,所以将a递增到8并将c分配给8

Same thing for line 3. 第3行也是如此。

While I'm not sure what you were expecting the outputs to be, I did see one thing that is probably not intentional. 虽然我不确定您期望的输出是什么,但我确实看到了一件事可能不是故意的。

Note that your function fn1 returns an int, however when you call it on line 20, it is not being used to update any of your variables. 请注意,函数fn1返回一个int,但是在第20行调用它时,该函数并未用于更新任何变量。 Perhaps you meant to update the value of y? 也许您打算更新y的值?

Also, you can get a better idea of how the function works by adding print statements to see how the internal variables iterate: 此外,通过添加打印语句以查看内部变量的迭代方式,您可以更好地了解该函数的工作方式:

int fn1(int &a, int b, int *c){
   a=b++;
   cout << "a: " << a << " ";
   b+=7;
   cout << "b: " << b << " ";
   *c= ++a;
   cout << "c: " << *c << endl;

   return b;
}

First output is pretty much simple, while in second output the only difference is that the return value of function ie 第一个输出非常简单,而在第二个输出中,唯一的区别是函数的返回值即

b+=7

is used to rewrite the original value of Y . 用于重写Y的原始值。 Then call the function again with new value of Y . 然后使用新值Y再次调用该函数。

In your int fn1(int &a, int b, int *c) function: 在您的int fn1(int &a, int b, int *c)函数中:

a is passed in by reference. a通过引用传递英寸
b is passed in by value. b按值传递。
c is passed in by pointer. c通过指针传递。

a = b++; modifies x because a is a reference of x . 修改x因为ax的引用。
b += 7; modifies b but not y because b is just the value of y . 修改b但不修改y因为b只是y的值。
*c = ++a; modifies x and z because a is a reference of x , and c is a pointer to z . 修改xz是因为ax的引用,而c是指向z的指针。

When you use references and pointers, you can write directly values into their own memory space. 使用引用和指针时,可以将值直接写入其自己的内存空间。 However, when you use by value (think of b ) a new variable is created within the current scope (inside of the fn1 function) and any changes to it are isolated to that scope. 但是,当您按值使用(认为b )时,会在当前范围内(在fn1函数内部)创建一个新变量,并且fn1任何更改都将隔离到该范围内。

Check out this SO answer to read more about scopes in C++. 查看此SO答案,以了解有关C ++范围的更多信息。 https://stackoverflow.com/a/11345787/1000437 https://stackoverflow.com/a/11345787/1000437

Hope this helps. 希望这可以帮助。

I'll try to explain. 我会尽力解释。 The pointers are really fun and powerful if you have enough patience: 如果您有足够的耐心,这些指针将非常有趣且功能强大:

fn1(x,y,&z);
cout<< x<< " " << y<< " "<< z<< endl; 
// output: 8 7 8 (I dont get this part!!!)

Let's take a look at our function fn1 让我们看一下函数fn1

int fn1(int &a, int b, int *c){
a=b++;
b+=7;
*c= ++a;

return b;
}

First, note that in fn1(int &a) variable a is passed by reference, not by value. 首先,请注意,在fn1(int &a)变量中,a是通过引用而不是通过值传递的。 This means that we will directly operate with the value that we pass on a. 这意味着我们将直接使用传递给a的值进行操作。 It basically means that if you change the value of a inside the function, the change will persist . 这基本上意味着,如果您更改函数内部的值,则更改将持续存在 So since int x=4, y=7, z=14; 所以既然int x=4, y=7, z=14; and we call fn1(x, y, &z) , we pass the Variable x to a (think of it as renaming it temporarily from x to a). 然后我们将其称为fn1(x, y, &z) ,将变量x传递给a(考虑将其从x临时重命名为a)。 So a is initially 4. b is simply a value, so the contents of y is passed to b and *c is a pointer and simply takes as value the physical address of z (that's why you have fn1(&z) ). 所以a最初是4。b只是一个值,因此y的内容传递给b,而* c是一个指针,并简单地将z的物理地址作为值(这就是为什么有fn1(&z) )。

So when we call our function fn1(x, y, &z) we execute: 因此,当我们调用函数fn1(x, y, &z)我们执行:

a=b++;

So a changes to 7, b changes to 8. (if this is confusing, read an intro about operators this ) Then: 因此,将a更改为7,将b更改为8。(如果这令人困惑,请阅读有关操作符的简介this )然后:

b+=7; //same as: b=b+7;

So b takes value 8+7=?? 所以b取值8 + 7 = ?? (sorry! I don't have a degree in Math!) (对不起!我没有数学学位!)

Finally: 最后:

*c= ++a;

Now, since we have ++a, first a changes to a=a+1 and now takes the value of 8, since it was 7! 现在,由于我们具有++ a,因此首先将a更改为a = a + 1,现在取值为8,因为它是7! And the *c means "the value of the variable that pointer c points to": so the value of the variable that c points to is z . * c表示“指针c指向的变量的值”:因此c指向的变量的值为z So the value of z is changed to whatever a is (which is 8). 因此,z的值更改为任何a(即8)。

And now the final step (for real this time). 现在是最后一步(这次是真实的)。 Why didn't y change? 为什么不y改变? Because we fn1 takes b by value, not by reference, and when the function ends, var b gets destroyed. 因为我们fn1通过值而不是通过引用取b,并且当函数结束时,var b被销毁。

Now try to do the second part: 现在尝试执行第二部分:

 y=fn1(x,y,&z);

In this case y will take whatever fn1 returns! 在这种情况下,y将取fn1返回的任何值! I'd really advise you take a step by step approach. 我真的建议您采取逐步的方法。 If it's still confusing, exercise separately with the behaviour of each type of var passing it by ref, by val and later by pointer. 如果仍然令人困惑,请分别练习每种类型的var的行为,分别通过ref,val和后来的指针传递它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM