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

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

What is the Difference When i write Func1(int &a) and Func1(int *a)

c++windowspass-by-referenceparameter-passing

提问by Simsons

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

可能的重复:
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:

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

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)

这两个函数都期望 a 的地址,但是当我调用 Func1 时,我这样做了Func1(a),在 Func2 的情况下,我调用了Func2(&a)

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

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

采纳答案by Chubsdad

Func1(int &a) 
// accepts arguments by reference.
// changes to a inside Func1 is reflected in the caller
// a cannot bind to an Rvalue e.g. can't call Func1(5)
// a can never be referring to something that is not a valid object

Func2(int *a)
// accept arguments by value
// change to a inside Func1 not reflected in caller, 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

回答by Nathan Pitman

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.

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

回答by EboMike

Internally, there's not much difference. But one is a reference, 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.

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

i.e.

IE

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

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

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

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

回答by Marek Szanyi

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

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

a Quick example :

一个简单的例子:

Reference : Object &obj

参考 : Object &obj

the same written with pointer syntax: Object* const obj

用指针语法写的一样: Object* const obj

回答by please delete me

Func1 will take a reference to an int, 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 de-referenced 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.

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

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.

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

Hope that helps.

希望有帮助。

回答by Mohamed Saligh

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 passing argument.

  2. Func2(int *a)- when yo 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 param '&a'.

  1. Func1(int &a)- 当你调用这个函数时Func1(a),它只会在那里传递 int 函数接收传递参数的地址。

  2. Func2(int *a)- 当你用 调用这个函数时Func2(&a),这个语句只是传递了 ' a'的引用。在被调用的函数参数 ' *a' 中,它将获得引用该调用函数的参数 ' &a' 的值。

回答by cpx

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

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

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

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

回答by Poonam Bhatt

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

当函数定义为 Func1(int &a) 时,表示该函数将接受将作为参数传递给该函数的变量地址。(因此您无需处理将参数传递给函数的变量地址)。函数默认行为是获取传递变量的地址。

e.g

例如

 Func1(int &a){
   a = 5; //a will store 5 at &a
}
Func1(a) // don't need to pass &a, function definition will take care this.

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

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

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

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

e.g

例如

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

function call: Fun2(&a); Must have to pass &a, function definition will NOT take care this.