C++ * vs & 在函数声明中

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

c++ * vs & in function declaration

c++pointersreferencepass-by-reference

提问by Paul Tarjan

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

可能的重复:
C++ 中指针变量和引用变量的区别

When should I declare my variables as pointers vs objects passed-by-reference? They compile to the same thing in assembly (at least run-time asymptotically) so when should I use which?

我什么时候应该将我的变量声明为指针和按引用传递的对象?它们在汇编中编译为相同的东西(至少在运行时渐近)所以我什么时候应该使用哪个?

void foo(obj* param)
void foo(obj& param)

采纳答案by Mykola Golubyev

My rule is simple: use * when you want to show that value is optional and thus can be 0.

我的规则很简单:当您想显示该值是可选的,因此可以为 0 时,请使用 *。

Excluding from the rule: all the _obj_s around are stored in containers and you don't want to make your code look ugly by using everywhere foo(*value);instead of foo(value);So then to show that value can't be 0 put assert(value);at the function begin.

从规则中排除:周围的所有 _obj_s 都存储在容器中,并且您不希望通过在任何地方使用foo(*value);而不是foo(value);So then 来使代码看起来丑陋,然后显示值不能为 0 放在assert(value);函数开始处。

回答by MahlerFive

I follow the Google style guide standardas it makes the most sense to me. It states:

我遵循Google 风格指南标准,因为它对我来说最有意义。它指出:

Within function parameter lists all references must be const:

void Foo(const string &in, string *out);

In fact it is a very strong convention in Google code that input arguments are values or const references while output arguments are pointers. Input parameters may be const pointers, but we never allow non-const reference parameters.

One case when you might want an input parameter to be a const pointer is if you want to emphasize that the argument is not copied, so it must exist for the lifetime of the object; it is usually best to document this in comments as well. STL adapters such as bind2nd and mem_fun do not permit reference parameters, so you must declare functions with pointer parameters in these cases, too.

在函数参数列表中,所有引用都必须是 const:

void Foo(const string &in, string *out);

事实上,在谷歌代码中输入参数是值或常量引用,而输出参数是指针,这是一个非常强的约定。输入参数可能是 const 指针,但我们绝不允许非常量引用参数。

您可能希望输入参数是 const 指针的一种情况是,如果您想强调该参数未被复制,因此它必须在对象的生命周期内存在;通常最好在评论中记录这一点。STL 适配器如 bind2nd 和 mem_fun 不允许引用参数,因此在这些情况下您也必须声明带有指针参数的函数。

回答by Greg Hewgill

One reason to use pointers is if it makes sense to pass a NULLvalue into the function. With a pointer, it's expected to be able to do this. With a reference, it is not expected to be able to do this.

使用指针的一个原因是NULL向函数传递一个值是否有意义。使用指针,预计能够做到这一点。有了参考,预计无法做到这一点。

(However, by doing tricky things it is still possible to pass a NULL into a reference parameter. You can expect that the called function may crash in this case.)

(但是,通过做一些棘手的事情,仍然可以将 NULL 传递给引用参数。在这种情况下,您可以预期被调用的函数可能会崩溃。)

Another convention is that if you pass a pointer into a function, the function may use the pointer to take ownership of the object (especially in a COM-like reference counted environment). If you pass a reference, then the called function can expect to use the object for the duration of the function call but not to keep a pointer to the object for use later.

另一个约定是,如果您将指针传递给函数,该函数可能会使用该指针来获取对象的所有权(尤其是在类似 COM 的引用计数环境中)。如果传递引用,则被调用的函数可以期望在函数调用期间使用该对象,但不会保留指向该对象的指针以供以后使用。

回答by quamrana

The other difference between pointers and references is that it is implied that you won't hold on to a reference, unless you pass one to a constructor. Passing pointers may mean that an object might hold onto it for a while, like a composite pattern object.

指针和引用之间的另一个区别是暗示您不会保留引用,除非您将引用传递给构造函数。传递指针可能意味着一个对象可能会保留一段时间,就像一个复合模式对象。

回答by elcuco

A good reason why I use pointers and not references in my C++ applications, is readability. When using a pointer you see what is really happening, and when using pointers the syntax tells you what's really happening.

我在 C++ 应用程序中使用指针而不是引用的一个很好的原因是可读性。使用指针时,您会看到实际发生的情况,而使用指针时,语法会告诉您实际发生的情况。

Same goes for "0" or "NULL". I prefer using "NULL", this way 3 months later when I see a code that looks like:

“0”或“NULL”也是如此。我更喜欢使用“NULL”,这样 3 个月后,当我看到如下代码时:

somevar_1 = 0;
somevar_2 = NULL;

I know that somevar_1is an int(or float) and somevar_2is some kind of pointer.

我知道这somevar_1是一个int(或float)并且somevar_2是某种指针。