c++ * 与 & 在函数声明中

发布于 2024-07-26 07:17:13 字数 361 浏览 4 评论 0原文

可能的重复:
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 技术交流群。

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

发布评论

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

评论(5

╄→承喏 2024-08-02 07:17:13

我的规则很简单:当你想表明该值是可选的,因此可以为 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 of foo(value);
So then to show that value can't be 0 put assert(value); at the function begin.

橘和柠 2024-08-02 07:17:13

我遵循 Google 样式指南标准,因为它最有意义大部头书。 它指出:

函数内参数列出所有
引用必须是 const:

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

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

您可能需要输入的一种情况
参数为 const 指针是 if
你想强调的是
参数没有被复制,所以它必须
在对象的生命周期内存在;
通常最好将其记录在
评论也是如此。 STL 适配器,例如
bind2nd 和 mem_fun 不允许
参考参数,所以你必须
用指针声明函数
这些情况下的参数也是如此。

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

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.

她如夕阳 2024-08-02 07:17:13

使用指针的原因之一是向函数传递 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.

玩物 2024-08-02 07:17:13

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

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.

君勿笑 2024-08-02 07:17:13

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

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

somevar_1 = 0;
somevar_2 = NULL;

我知道 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:

somevar_1 = 0;
somevar_2 = NULL;

I know that somevar_1 is an int (or float) and somevar_2 is some kind of pointer.

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