C++ 引用调用和指针调用的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17423218/
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
Diff Between Call By Reference And Call By Pointer
提问by Vinoth Babu
Can any one tell me the exact diff between call by pointer and call by reference. Actually what is happening on both case?
任何人都可以告诉我按指针调用和按引用调用之间的确切区别。实际上这两种情况都发生了什么?
Eg:
例如:
Call By Reference:
参考调用:
void swap(int &x, int &y)
{
int temp;
temp = x; /* save the value at address x */
x = y; /* put y into x */
y = temp; /* put x into y */
return;
}
swap(a, b);
Call By Pointer:
指针调用:
void swap(int *x, int *y)
{
int temp;
temp = *x; /* save the value at address x */
*x = *y; /* put y into x */
*y = temp; /* put x into y */
return;
}
swap(&a, &b);
回答by Spook
The both cases do exactly the same.
这两种情况完全一样。
However, the small difference is, that references are never null(and inside function you are sure, that they are referencing valid variable). On the other hand, pointers may be empty or may point to invalid place in memory, causing an AV.
但是,微小的区别是,引用永远不会为空(并且您确定在函数内部,它们引用的是有效变量)。另一方面,指针可能为空或可能指向内存中的无效位置,从而导致 AV。
回答by Daniel Gratzer
Semantically these calls have identical results; under the hood references are implemented with pointers.
从语义上讲,这些调用具有相同的结果;引擎盖下的引用是用指针实现的。
The important difference between using references and pointers is with references you have a lot less rope to hang yourself with. References are always pointing to "something", where as pointers can point to anything. For example, it is perfectly possible to do something like this
使用引用和指针之间的重要区别在于,使用引用时,您可以挂起自己的绳索要少得多。引用总是指向“某物”,而指针可以指向任何东西。例如,完全有可能做这样的事情
void swap(int *x, int *y)
{
int temp;
temp = *x; /* save the value at address x */
++x;
*x = *y; /* put y into x */
*y = temp; /* put x into y */
return;
}
And now this code could do anything, crash, run, have monkeys fly out of your noes, anything.
现在这段代码可以做任何事情,崩溃,运行,让猴子飞出你的视线,任何事情。
When in doubt, prefer references. They are a higher, safer level of abstraction on pointers.
如有疑问,请优先参考。它们是对指针的更高、更安全的抽象级别。
回答by moony
Your code is C++ code. In Java there are no pointers. Both examples do the same thing, functionally there is no difference.
您的代码是 C++ 代码。在 Java 中没有指针。两个示例都做同样的事情,功能上没有区别。
回答by user2497624
I don't know exactly why this was tagged jave as there are no pointers. However, for the concept you can use both pointers and reference interchangeably if you are working on the value attribute. for example i=4 or *i=4. The advantage of using pointers is that you can manipulate the adress itself. Sometimes you will need to modify the adress (point it to something else ...) this is when pointers are different for reference; pointers allow you to work directly on the adress, reference won't
我不知道为什么这被标记为 jave,因为没有指针。但是,对于这个概念,如果您正在处理 value 属性,则可以互换使用指针和引用。例如 i=4 或 *i=4。使用指针的优点是您可以操纵地址本身。有时您需要修改地址(将其指向其他内容......)这是指针不同以供参考的时候;指针允许您直接处理地址,引用不会
回答by Valeri Atamaniouk
In your example, there is no difference.
在您的示例中,没有区别。
In C++ reference is implemented using pointers.
在 C++ 中,引用是使用指针实现的。
Edit: for those who believe reference can't be NULL:
编辑:对于那些相信引用不能为 NULL 的人:
int calc(int &arg)
{
return arg;
}
int test(int *arg)
{
return calc(*arg);
}
Guess, what will be the result for test(0)
?
猜猜,结果会怎样test(0)
?