按值传递结果? [英] Pass-by-value-Result?

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

问题描述


可能的重复项

通过引用传递或传递值?

按参考/ C ++中的值传递

Im与pass-by-value-result方法有问题。我理解通过引用和值传递,但不完全清晰的值传递结果。

Im having trouble with the pass-by-value-result method. i understand pass by reference and pass by value but im not quite clear on pass-by-value result. How similar is it to pass by value(assuming it is similar)?

这里是代码

#include <iostream>
#include <string.h>
using namespace std;

void swap(int a, int b)
{

  int temp;
    temp = a;
    a = b;
    b = temp;
}

int main()
{
  int value = 2;
  int  list[5] = {1, 3, 5, 7, 9};


  swap(value, list[0]);

  cout << value << "   " << list[0] << endl;

  swap(list[0], list[1]);

  cout << list[0] << "   " << list[1] << endl;

  swap(value, list[value]);

  cout << value << "   " << list[value] << endl;

}

现在的目标是找出什么是value和list是如果使用pass by value结果。 (不通过值)。

now the objective is to find out what is the value of "value" and "list" are if you use pass by value result. (NOT pass by value).

推荐答案

如果你传递的值,然后你复制的方法。这意味着对该变量所做的任何更改都不会发生在原始变量中。这意味着您的输出如下:

If you're passing by value then you're copying the variable over in the method. Which means any changed made to that variable don't happen to the original variable. This means your output would be as follows:

2   1
1   3
2   5

如果你通过引用传递变量的地址(而不是复制),那么你的输出将是不同的,并且将反映在交换(int a,int b)中进行的计算。你这样做是为了检查结果吗?

If you were passing by reference, which is passing the address of your variable (instead of making a copy) then your output would be different and would reflect the calculations made in swap(int a, int b). Have you ran this to check the results?

编辑
做了一些研究后,我发现了一些东西。 C ++不支持Pass-by-value-result,但它可以被模拟。为此,您创建一个变量的副本,通过引用您的函数传递它们,然后将您的原始值设置为临时值。请参阅下面的代码。

EDIT After doing some research I found a few things. C++ Does not support Pass-by-value-result, however it can be simulated. To do so you create a copy of the variables, pass them by reference to your function, and then set your original values to the temporary values. See code below..

#include <iostream>
#include <string.h>
using namespace std;

void swap(int &a, int &b)
{

  int temp;
    temp = a;
    a = b;
    b = temp;
}

int main()
{
  int value = 2;
  int  list[5] = {1, 3, 5, 7, 9};


  int temp1 = value;
  int temp2 = list[0]

  swap(temp1, temp2);

  value = temp1;
  list[0] = temp2;

  cout << value << "   " << list[0] << endl;

  temp1 = list[0];
  temp2 = list[1];

  swap(list[0], list[1]);

  list[0] = temp1;
  list[1] = temp2;

  cout << list[0] << "   " << list[1] << endl;

  temp1 = value;
  temp2 = list[value];

  swap(value, list[value]);

  value = temp1;
  list[value] = temp2;
  cout << value << "   " << list[value] << endl;

}

这将给你的结果:

1   2
3   2
2   1

这种类型的传递也称为复制输入,复制输出。 Fortran使用它。但这是我在我的搜索过程中发现的。希望这有助于。

This type of passing is also called Copy-In, Copy-Out. Fortran use to use it. But that is all I found during my searches. Hope this helps.

这篇关于按值传递结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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