** 和 *& 在参数传递中的 C++ 区别

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

C++ difference between ** and *& in parameter passing

c++

提问by bb2

I have implemented operations on a list, one of them is add, and since i don't want to return anything, i read that i had to use **, and it works, but i saw on another place that it is passed as *&, but i don't know the difference

我已经在一个列表上实现了操作,其中一个是添加,因为我不想返回任何东西,我读到我必须使用 **,它可以工作,但我在另一个地方看到它作为*&,但我不知道有什么区别

addNode( node *&head, int value) addNode(node **head, int value)

addNode(node *&head, int value) addNode(node **head, int value)

What is the difference, and which one is better, or do they mean the same? I know the second is pointer to a pointer.

有什么区别,哪个更好,或者它们的意思相同?我知道第二个是指向指针的指针。

Thanks

谢谢

回答by Mark Byers

The first (**) is a pointerto a pointer and the second (*&) is a referenceto a pointer.

第一个 ( **) 是指向指针的指针,第二个 ( *&) 是对指针的引用

A reference and a pointer are conceptually quite similar. But there are some important differences, for example:

引用和指针在概念上非常相似。但是有一些重要的区别,例如:

  • A reference cannot be NULL (but it can refer to a pointer which points to NULL).
  • You can't modify a reference to refer to something else.
  • You need to dereference a pointer to access the value.
  • 引用不能为 NULL(但它可以引用指向 NULL 的指针)。
  • 您不能修改引用以引用其他内容。
  • 您需要取消引用指针才能访问该值。

See this related question for more differences:

有关更多差异,请参阅此相关问题:

回答by John Dibling

With:

和:

addNode( node *&head, int value)

...the type of headis "reference to pointer-to-node".

...的类型head是“对指向节点的指针的引用”。

With:

和:

addNode(node **head, int value)

... the type is "pointer-to-pointer-to-node".

...类型是“指针到指针到节点”。

A pointer and a reference are notthe same thing. A simple way to think of a reference is as a dereferenced pointer.

指针和引用不是一回事。将引用视为取消引用的指针的一种简单方法。

You would need different syntax to call both versions:

您需要不同的语法来调用两个版本:

node* my_node = 0;
addNode(my_node, 0);  // syntax for first version
addNode(&my_node, 0); // syntax for 2nd version

There are semantic differences as well. When you pass a pointer, you can pass NULL. When you pass a reference, you can't. This being a function that takes a ref-to-ptr confuses the matter a little, so let's change the problem a bit:

也存在语义差异。传递指针时,可以传递NULL。当你传递一个引用时,你不能。这是一个需要 ref-to-ptr 的函数,这有点混淆了这个问题,所以让我们稍微改变一下问题:

void make_string(string& str_ref)
{
  str_ref = "my str";
}

void make_string_again(string* str_ptr)
{
  *str_ptr = "my other string";
}

These two finctions do the same thing but one takes a stringreference while the other takes a stringpointer. If you were to do this:

这两个函数做同样的事情,但一个接受一个string引用,而另一个接受一个string指针。如果你要这样做:

string str;
make_string(str); // OK
make_string_again(&str); // OK - &str is a pointer to a real string
make_string_again(0); // Not OK - compiles but will crash when function dereferences the null pointer

You can see it becomes difficult (but not impossible) to call make_stringwith a null pointer. This could help you to implement better functions in the case where you expect that make_stringwill neverbe called with an invalid object.

您可以看到make_string使用空指针调用变得困难(但并非不可能)。这可以帮助你,你希望实现的情况下更好的功能make_string永远不会有一个无效的对象调用。

回答by Bill Lynch

This is the difference between pass by valueand pass by reference. Pass by reference, basically, implicitly does the reference and dereference that the double pointer gets you.

这就是传值传引用的区别。通过引用传递,基本上,隐式地执行双指针为您提供的引用和取消引用。