C++ 指针传递和引用传递

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8571078/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 18:49:03  来源:igfitidea点击:

Pass by pointer & Pass by reference

c++pointersreferenceparameter-passing

提问by cppcoder

Possible Duplicate:
What are the differences between pointer variable and reference variable in C++?
Are there benefits of passing by pointer over passing by reference in C++?

可能的重复:
C++ 中的指针变量和引用变量有什么区别?
在 C++ 中通过指针传递比通过引用传递有好处吗?

In both cases, I achieved the result. So when is one preferred over the other? What are the reasons we use one over the other?

在这两种情况下,我都达到了结果。那么什么时候一个比另一个更受欢迎呢?我们使用一个而不是另一个的原因是什么?

#include <iostream>
using namespace std;
void swap(int* x, int* y)
{
    int z = *x;
    *x=*y;
    *y=z;
}
void swap(int& x, int& y)
{
    int z = x;
    x=y;
    y=z;
}

int main()
{
    int a = 45;
    int b = 35;
    cout<<"Before Swap\n";
    cout<<"a="<<a<<" b="<<b<<"\n";

    swap(&a,&b);
    cout<<"After Swap with pass by pointer\n";
    cout<<"a="<<a<<" b="<<b<<"\n";

    swap(a,b);
    cout<<"After Swap with pass by reference\n";
    cout<<"a="<<a<<" b="<<b<<"\n";
}

Output

输出

Before Swap
a=45 b=35
After Swap with pass by pointer
a=35 b=45

After Swap with pass by reference
a=45 b=35

采纳答案by moshbear

A reference is semantically the following:

引用在语义上如下:

T& <=> *(T * const)

T& <=> *(T * const)

const T& <=> *(T const * const)

const T& <=> *(T const * const)

T&& <=> [no C equivalent](C++11)

T&& <=> [no C equivalent](C++11)

As with other answers, the following from the C++ FAQ is the one-line answer: references when possible, pointers when needed.

与其他答案一样,C++ 常见问题解答中的以下内容是单行答案:可能时引用,需要时使用指针。

An advantage over pointers is that you need explicit casting in order to pass NULL. It's still possible, though. Of the compilers I've tested, none emit a warning for the following:

与指针相比的一个优势是您需要显式转换才能传递 NULL。不过还是有可能的。在我测试过的编译器中,没有一个会发出以下警告:

int* p() {
    return 0;
}
void x(int& y) {
  y = 1;
}
int main() {
   x(*p());
}

回答by moshbear

In fact, most compilers emit the same code for both functions calls, because references are generally implemented using pointers.

事实上,大多数编译器为两个函数调用发出相同的代码,因为引用通常是使用指针实现的。

Following this logic, when an argument of (non-const) reference type is used in the function body, the generated code will just silently operate on the address of the argument and it will dereference it. In addition, when a call to such a function is encountered, the compiler will generate code that passes the address of the arguments instead of copying their value.

按照这个逻辑,当在函数体中使用(非常量)引用类型的参数时,生成的代码只会对参数的地址进行静默操作,并且会取消引用它。此外,当遇到对此类函数的调用时,编译器将生成传递参数地址而不是复制其值的代码。

Basically, references and pointers are not very different from an implementation point of view,the main (and very important) difference is in the philosophy: a reference is the object itself,just with a different name.

基本上,从实现的角度来看,引用和指针并没有太大区别主要(也是非常重要)的区别在于哲学:引用是对象本身,只是名称不同。

References have a couple more advantages compared to pointers (e. g. they can't be NULL, so they are safer to use). Consequently, if you can use C++, then passing by reference is generally considered more elegant and it should be preferred. However, in C, there's no passing by reference, so if you want to write C code (or, horribile dictu, code that compiles with both a C and a C++ compiler, albeit that's not a good idea), you'll have to restrict yourself to using pointers.

与指针相比,引用具有更多优势(例如,它们不能是NULL,因此使用起来更安全)。因此,如果您可以使用 C++,那么通过引用传递通常被认为更优雅,应该是首选。然而,在 C 中,没有通过引用传递,所以如果你想编写 C 代码(或者,可怕的口号,用 C 和 C++ 编译器编译的代码,尽管这不是一个好主意),你必须限制自己使用指针。

回答by Mark Ransom

Pass by pointer is the only way you could pass "by reference" in C, so you still see it used quite a bit.

通过指针传递是您可以在 C 中“通过引用”传递的唯一方法,因此您仍然会看到它使用了很多。

The NULL pointer is a handy convention for saying a parameter is unused or not valid, so use a pointer in that case.

NULL 指针是一个方便的约定,用于表示参数未使用或无效,因此在这种情况下使用指针。

References can't be updated once they're set, so use a pointer if you ever need to reassign it.

引用一旦设置就无法更新,因此如果您需要重新分配它,请使用指针。

Prefer a reference in every case where there isn't a good reason not to. Make it constif you can.

在没有充分理由不这样做的每种情况下,都更喜欢参考。可以的const话就做吧。

回答by Rocky

Hereis a good article on the matter - "Use references when you can, and pointers when you have to."

这里有一篇关于这个问题的好文章——“尽可能使用引用,必要时使用指针。”

回答by Sanish

Use references all the time and pointers only when you have to refer to NULLwhich reference cannot refer.

始终使用引用,仅在必须NULL引用不能引用的引用时才使用指针。

See this FAQ : http://www.parashift.com/c++-faq-lite/references.html#faq-8.6

请参阅此常见问题解答:http: //www.parashift.com/c++-faq-lite/references.html#faq-8.6