c++ * 与 & 在函数声明中
可能的重复:
C++中指针变量和引用变量的区别 < /p>
我应该将变量声明为指针还是按引用传递的对象? 它们在汇编中编译为相同的东西(至少在运行时渐近),所以我什么时候应该使用哪个?
void foo(obj* param)
void foo(obj& param)
Possible Duplicate:
Difference between pointer variable and reference variable in 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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我的规则很简单:当你想表明该值是可选的,因此可以为 0 时,请使用 *。
从规则中排除:周围的所有 _obj_ 都存储在容器中,并且你不希望在任何地方使用都使你的代码看起来很难看
foo(*value);
而不是foo(value);
因此,为了表明 value 不能为 0,请在函数开始处放置
assert(value);
。My rule is simple: use * when you want to show that value is optional and thus can be 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 offoo(value);
So then to show that value can't be 0 put
assert(value);
at the function begin.我遵循 Google 样式指南标准,因为它最有意义大部头书。 它指出:
I follow the Google style guide standard as it makes the most sense to me. It states:
使用指针的原因之一是向函数传递 NULL 值是否有意义。 有了指针,预计就能够做到这一点。 有了参考,估计做不到这一点。
(但是,通过做一些棘手的事情,仍然可以将 NULL 传递到引用参数中。在这种情况下,您可以预期被调用的函数可能会崩溃。)
另一个约定是,如果将指针传递到函数中,则该函数可能会 崩溃。使用指针获取对象的所有权(尤其是在类似 COM 的引用计数环境中)。 如果传递引用,则被调用函数可以在函数调用期间使用该对象,但不会保留指向该对象的指针以供以后使用。
One reason to use pointers is if it makes sense to pass a
NULL
value 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.(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.)
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.
指针和引用之间的另一个区别是,它意味着您不会保留引用,除非您将引用传递给构造函数。 传递指针可能意味着对象可能会保留它一段时间,就像复合模式对象一样。
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.
我在 C++ 应用程序中使用指针而不是引用的一个很好的原因是可读性。 当使用指针时,您会看到真正发生的事情,而当使用指针时,语法会告诉您真正发生的事情。
“0”或“NULL”也是如此。 我更喜欢使用“NULL”,这样 3 个月后,当我看到如下代码时:
我知道
somevar_1
是一个int
(或float
code>) 和 somevar_2 是某种指针。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.
Same goes for "0" or "NULL". I prefer using "NULL", this way 3 months later when I see a code that looks like:
I know that
somevar_1
is anint
(orfloat
) andsomevar_2
is some kind of pointer.