通过指针&通过引用传递

发布于 2024-12-22 01:06:50 字数 1224 浏览 0 评论 0原文

可能的重复:
指针变量和C++ 中的引用变量?
路过有好处吗C++ 中通过引用传递指针?

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

#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";
}

输出

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

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++?

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

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

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

发布评论

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

评论(5

各自安好 2024-12-29 01:06:51

通过指针传递是在 C 中“通过引用”传递的唯一方法,因此您仍然会看到它被大量使用。

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

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

在没有充分理由的情况下,最好有一个参考。如果可以的话,将其设为 const

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

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

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 const if you can.

护你周全 2024-12-29 01:06:51

这里是一篇关于这个问题的好文章 - “尽可能使用参考文献,并在必要时提供指导。”

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

記憶穿過時間隧道 2024-12-29 01:06:51

始终使用引用,仅当必须引用引用无法引用的 NULL 时才使用指针。

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

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

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

时常饿 2024-12-29 01:06:50

引用在语义上如下:

T& <=> *(T * const)

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

T&& <=> [没有 C 等效项] (C++11)

与其他答案一样,C++ FAQ 中的以下内容是一行答案:可能时引用,需要时指针。

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

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

A reference is semantically the following:

T& <=> *(T * const)

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

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.

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:

int* p() {
    return 0;
}
void x(int& y) {
  y = 1;
}
int main() {
   x(*p());
}
所有深爱都是秘密 2024-12-29 01:06:50

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

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

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

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

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.

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