C++ 通过引用传递还是通过指针传递?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5893873/
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
Pass by reference vs pass by pointer?
提问by mikhailovich
Possible Duplicate:
When to pass by reference and when to pass by pointer in C++?
What is the difference between passing by reference and passing the value by a pointer?
通过引用传递和通过指针传递值有什么区别?
回答by Rudi
When you pass a parameter by reference, the parameter inside the function is an alias to the variable you passed from the outside. When you pass a variable by a pointer, you take the address of the variable and pass the address into the function. The main difference is that you can pass values without an address (like a number) into a function which takes a const reference, while you can't pass address-less values into a function which takes const pointers.
当您通过引用传递参数时,函数内部的参数是您从外部传递的变量的别名。当您通过指针传递变量时,您获取变量的地址并将地址传递给函数。主要区别在于您可以将没有地址的值(如数字)传递给采用 const 引用的函数,而您不能将无地址的值传递给采用 const 指针的函数。
Typically a C++ compiler implement a reference as a hidden pointer.
通常,C++ 编译器将引用实现为隐藏指针。
You can change your function into the pointer variant this way:
您可以通过这种方式将函数更改为指针变量:
void flip(int *i) // change the parameter to a pointer type
{
cout << " flip start "<<"i="<< *i<<"\n"; // replace i by *i
*i = 2*(*i); // I'm not sure it the parenthesis is really needed here,
// but IMHO this is better readable
cout << " flip exit "<<"i="<< *i<<"\n";
}
int main()
{
int j =1;
cout <<"main j="<<j<<endl;
flip(&j); // take the address of j and pass this value
// adjust all other references ...
}
回答by Pavan Yalamanchili
For the second part of your question, here is the code.
对于您问题的第二部分,这里是代码。
#include <iostream>
#include <cassert>
using namespace std;
void flip(int *i)
{
cout << " flip start "<<"i="<< i<<"\n";
*i *= 2;
cout << " flip exit "<<"i="<< i<<"\n";
}
int main()
{
int j =1;
cout <<"main j="<<j<<endl;
flip(&j);
cout <<"main j="<<j<<endl;
flip(&j);
cout <<"main j="<<j<<endl;
flip(&j);
cout <<"main j="<<j<<endl;
assert(j==8);
return 0;
}
For the first part of your question, I am new to C++ but I find it useful to pass by pointer when having to return multiple outputs for a function. Or to pass NULL as a parameter.
对于您问题的第一部分,我是 C++ 新手,但我发现在必须为函数返回多个输出时传递指针很有用。或者将 NULL 作为参数传递。
回答by rongruspe
technically, you just need put an asterisk before the variable name to pass by reference ;) it will then now pass the address of where your variable is in your memory.
从技术上讲,你只需要在变量名前放一个星号就可以通过引用传递;) 然后它现在将传递变量在内存中的地址。
now the difference of pass by reference and pass by value is just simple. think of it this way.. imagine yourself trying to give a candy to your friend.
现在传引用和传值的区别就很简单了。这么想吧……想象一下你想给你的朋友一颗糖果。
if you pass by value.. you: hey, i'm gonna give you something.. friend: what is it? you: here friend: thanks xD
如果你通过价值传递.. 你:嘿,我要给你一些东西.. 朋友:这是什么?你:在这里 朋友:谢谢 xD
if you pass by reference.. you: hey, i'm gonna give you something.. friend: what is it? you: it's on the right side of the table where the cookie jar is friend: thanks xD
如果你通过引用传递.. 你:嘿,我要给你一些东西.. 朋友:这是什么?你:它在桌子的右边,饼干罐是朋友:谢谢 xD
if you pass by value, your friend doesn't know where the candy came from. it may come from the store, the fridge, or wherever. now if you pass by reference, your friend doesnt know what is it that you are gonna give him.
如果你通过值传递,你的朋友不知道糖果是从哪里来的。它可能来自商店、冰箱或任何地方。现在如果你通过引用传递,你的朋友不知道你要给他什么。
to relate it with programming, the candy is the value of the variable. the instruction "it's on the right side of the table where the cookie jar is" is the memory address of where the value of your variable is located. you are very much going to use this in data structures so yeah :) hope i helped you in any way xD
将它与编程联系起来,糖果就是变量的值。指令“它在cookie jar所在的表的右侧”是变量值所在的内存地址。你非常会在数据结构中使用它,所以是的 :) 希望我以任何方式帮助你 xD