C++通过参考计划

发布于 2024-09-13 04:59:12 字数 614 浏览 10 评论 0原文

IBM 在下面的示例中通过引用解释了 C++ 传递(包含源代码)。

如果我将 void swapnum... 更改为 void swapnum(int i, int j),它会变成按值传递吗?

// pass by reference example
// author - ibm

#include <stdio.h>

void swapnum(int &i, int &j) {
  int temp = i;
  i = j;
  j = temp;
}

int main(void) {
  int a = 10;
  int b = 20;

  swapnum(a, b);
  printf("A is %d and B is %d\n", a, b);
  return 0;
}

来源

IBM explains C++ pass by reference in the example below (source included).

If I changed void swapnum... to void swapnum(int i, int j), would it become pass by value?

// pass by reference example
// author - ibm

#include <stdio.h>

void swapnum(int &i, int &j) {
  int temp = i;
  i = j;
  j = temp;
}

int main(void) {
  int a = 10;
  int b = 20;

  swapnum(a, b);
  printf("A is %d and B is %d\n", a, b);
  return 0;
}

Source

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

苍景流年 2024-09-20 04:59:12

如果按值传递,则执行的任何交换只会在它们传递到的函数中受到影响或看到,而不会在调用代码中受到影响。另外,一旦你返回到main,你会发现a和b没有交换。这就是为什么在交换数字时你想通过 ref 传递。

如果您只是问这是否就是它的名称,那么是的,您是对的,您将按值传递。

这是一个示例:

#include <stdio.h>

void swapnum(int &i, int &j) {
  int temp = i;
  i = j;
  j = temp;
}

void swapByVal(int i, int j) {
  int temp = i;
  i = j;
  j = temp;
}

int main(void) {
  int a = 10;
  int b = 20;

  swapnum(a, b);
  printf("swapnum A is %d and B is %d\n", a, b);

  swapByVal(a, b);
  printf("swapByVal A is %d and B is %d\n", a, b);

  return 0;
}

运行此代码,您应该看到更改仅持续存在通过引用交换,即在我们返回到 main 后,值被交换。如果按值传递,您将看到调用此函数并返回到 main,实际上 a 和 b 没有交换。

Any swapping performed if you pass by value are only affected or seen within the function they are passed into and not the calling code. In addition, once you return back to main you will see that a and b did not swap. That is why when swapping numbers you want to pass by ref.

If you are just asking if that is what it would be called, then yes you are right, you would be passing by value.

Here is an example:

#include <stdio.h>

void swapnum(int &i, int &j) {
  int temp = i;
  i = j;
  j = temp;
}

void swapByVal(int i, int j) {
  int temp = i;
  i = j;
  j = temp;
}

int main(void) {
  int a = 10;
  int b = 20;

  swapnum(a, b);
  printf("swapnum A is %d and B is %d\n", a, b);

  swapByVal(a, b);
  printf("swapByVal A is %d and B is %d\n", a, b);

  return 0;
}

Run this code and you should see that changes persist only by swapping by reference, that is after we've returned back to main the values are swapped. If you pass by value, you will see that calling this function and returning back to main that in fact a and b did not swap.

所谓喜欢 2024-09-20 04:59:12

是的,但这意味着 swapnum 将不再像名称所暗示的那样工作

Yes, but that means that swapnum will no longer work as the name implies

少女情怀诗 2024-09-20 04:59:12

是的。 '&'运算符指定参数应指向传入内容的内存地址。

通过删除此运算符,将创建对象的副本并将其传递给函数。

Yes. The '&' operator specifies that the parameter should point to the memory address of what was passed in.

By removing this operator, a copy of the object is made and passed to the function.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文