我写Func1(int &a) 和Func1(int *a) 有什么区别?

发布于 2024-10-03 16:36:18 字数 642 浏览 7 评论 0原文

可能的重复:
C++中指针变量和引用变量之间的区别< /p>

我从 C++ 开始,我发现下面的操作令人困惑。我了解了按引用传递和按值传递。但最近我遇到了这样的函数,这让我很困惑:

Func1(int &a)
Func2(int *a)

这两个函数都期望 a 的地址,但是当我调用 Func1 时,我通过 Func1(a) 来实现这一点,对于 Func2,我通过 Func2(&a) 进行调用。

为什么 Func1 在期待 a 的地址时直接接受 int a

Possible Duplicate:
Difference between pointer variable and reference variable in C++

As I am starting with C++, I found the operation below confusing. I got to know about passing by reference and passing by value. But recently I came across functions like this which confused me:

Func1(int &a)
Func2(int *a)

Both of the functions expect the address of a, but when I call Func1 I do that by Func1(a) and in case of Func2, I call by Func2(&a).

How come Func1 is accepting int a directly while it is expecting the address of a?

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

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

发布评论

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

评论(8

伪心 2024-10-10 16:36:18
Func1(int &a)
// It accepts arguments by reference.
// Changes to an inside Func1 are reflected in the caller.
// 'a' cannot bind to an Rvalue, e.g., it can't call Func1(5)
// 'a' can never be referring to something that is not a valid object

Func2(int *a)
// It accepts arguments by value.
// Change to an inside Func1 is not reflected in the caller, and changes to *a are
// 'a' can bind to an Rvalue, e.g., Func1(&localvar).
// 'a' can be NULL. Hence Func2 may need to check if 'a' is NULL.
Func1(int &a)
// It accepts arguments by reference.
// Changes to an inside Func1 are reflected in the caller.
// 'a' cannot bind to an Rvalue, e.g., it can't call Func1(5)
// 'a' can never be referring to something that is not a valid object

Func2(int *a)
// It accepts arguments by value.
// Change to an inside Func1 is not reflected in the caller, and changes to *a are
// 'a' can bind to an Rvalue, e.g., Func1(&localvar).
// 'a' can be NULL. Hence Func2 may need to check if 'a' is NULL.
固执像三岁 2024-10-10 16:36:18

当为按引用传递参数提供参数时,编译器将执行必要的 & 操作。幕后操作。作为程序员,您知道它在内部使用您的参数的地址,但这隐藏在按引用传递抽象中。这比使用指针更安全,因为您不会无意中重新分配引用。

When providing an argument for a pass-by-reference parameter, the compiler will do the necessary & operation behind the scenes. You, as the programmer, know that it is internally using the address of your argument, but this is hidden in the pass-by-reference abstraction. This is safer than using a pointer as you cannot inadvertently reassign a reference.

浮世清欢 2024-10-10 16:36:18

内部来说,没有太大区别。但一个是引用,另一个是指针。主要区别在于您无法修改函数中的引用,因此该引用将始终指向 a

即,

void func1(int &a) {
    a = 5;
}

这将修改 a,即调用者指向的变量。

void func2(int *a) {
    *a = 5;   // Same effect as the above code
    a = &b;   // You couldn't do that with a reference
}

Internally, there's not much difference. But one is a reference, and the other one is a pointer. The primary difference is that you can't modify the reference in your function, so the reference will always point to a.

I.e.,

void func1(int &a) {
    a = 5;
}

This will modify a, i.e., whichever variable the caller pointed to.

void func2(int *a) {
    *a = 5;   // Same effect as the above code
    a = &b;   // You couldn't do that with a reference
}
孤芳又自赏 2024-10-10 16:36:18

基本上,当你有一个&something时,你就有了一个引用,这只不过是一个不能改变的指针。 ,它是一个 const 指针,所以基本上它就像 *something,但在这种情况下,您可以更改指针(指向其他地方):)

换句话说 示例:

参考:Object &obj

与指针语法相同:Object* const obj

Basically, when you have a &something you have a reference and this is nothing more than a pointer which cannot change. In other words, it is a const pointer, so basically it’s like *something, but in this case you can change the pointer (to point somewhere else) :)

A quick example:

Reference: Object &obj

The same written with pointer syntax: Object* const obj

柠檬 2024-10-10 16:36:18

Func1 将采用对 int 的引用,Func2 将采用指向 int 的指针。当您执行Func2(&someint)时,您为函数提供了someint的地址。可以取消引用该地址以获取其值。当您“按值传递” Func1(int someint) 时,将执行 someint 的副本。当您通过引用或指针传递时,不会执行此类复制。

引用本质上可以被认为是原始值的“别名”,或者是引用它的另一种方式。是的,它是抽象的,但是,其他一切都是特定于实现的,不需要您担心。

Func1 will take a reference to an int, and Func2 will take a pointer to an int. When you do Func2(&someint), you're giving the function the address of someint. This address can be dereferenced to get its value. When you "pass by value" Func1(int someint), then, a copy of someint is performed. When you pass either by reference, or by pointer, no such copy is performed.

A reference can be thought of, as essentially an "alias" to the original value, or, another way of referring to it. Yes, it's abstract, but, everything else is implementation-specific and not for you to worry about.

树深时见影 2024-10-10 16:36:18

Func1(int &a) 中,&a 是对要传递的变量的引用。

Func2(int *a) 中,*a 是指向您将通过其地址传递的变量地址的指针。

In Func1(int &a), &a is the reference to the variable that you will be passing.

In Func2(int *a), *a is the pointer to address of the variable that you will be passing by its address.

失去的东西太少 2024-10-10 16:36:18

当函数定义为 Func1(int &a) 时,这意味着该函数将接受变量的地址,该变量将作为参数传递给该函数。 (所以你不需要关心传递将作为函数参数的变量的地址)。函数的默认行为是获取传递变量的地址。

例如,

Func1(int &a){
   a = 5; // 'a' will store 5 at &a
}

Func1(a) // We don't need to pass &a. The function definition will take care of this.

==================================================== ===================

如果函数定义是 Func2(int *a),则意味着它将保存给定值的地址。这意味着您必须在函数调用中传递 &a 作为参数,该参数稍后将存储在函数定义中的 *a 中。

例如,

Fun2(int *a){
   *a = 7; // a will store 7 at &a
}

函数调用:Fun2(&a);必须要通过&a。函数定义将不会处理这个问题。

When the function definition is Func1(int &a), it means this function will accept the address of the variable which will pass to this function as a parameter. (So you don't need to take care of passing the address of the variable which will a parameter to the function). The function default behavior will be to get the address of the passed variable.

E.g.,

Func1(int &a){
   a = 5; // 'a' will store 5 at &a
}

Func1(a) // We don't need to pass &a. The function definition will take care of this.

===================================================================

Whereas if the function definition is Func2(int *a), it means it will hold the address of the given value. That means you must have to pass &a in the function call as a parameter, which will be later stored as in *a in the function definition.

E.g.,

Fun2(int *a){
   *a = 7; // a will store 7 at &a
}

Function call: Fun2(&a); Must have to pass &a. The function definition will not take care of this.

拥抱影子 2024-10-10 16:36:18

事实并非如此。

  1. Func1(int &a) - 当您调用此函数时,Func1(a) 只会在此处传递 int,该函数接收传递参数的地址。

  2. Func2(int *a) - 当您使用 Func2(&a) 调用此函数时,此语句仅传递 'a< 的引用/代码>'。在被调用函数参数“*a”中,它将获得引用调用函数参数“&a”的值。


It is not like that.

  1. Func1(int &a) - when you call this function, Func1(a) which will pass the int only there, the function receives the address of the passing argument.

  2. Func2(int *a) - when you call this function with Func2(&a), this statement just passes the reference of 'a'. In the called function argument '*a' which will gain the value which is referring that calling function's parameter '&a'.

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