在引用 (T&) 和 const 指针 (T* const) 之间进行选择

发布于 2024-12-07 13:00:23 字数 426 浏览 1 评论 0原文

是否有任何合理用例,应该使用const指针而不是引用?

T obj;
T &r = obj;  // style-1
T* const p = &obj; // style-2

两种样式都可以用于相同的目的。我总是更喜欢代码中的第一种样式,并将后一种样式视为已弃用。但是我仍然想知道是否错过了第二种风格更好的用例?

编辑:不限于上面的例子,我在更广泛的意义上谈论,

void foo (T& r); // style-1
void foo (T* const p); // style-2

[我从几个答案中看到,style-2允许传递null。]

Is there any reasonable use case, where one should use const pointer over reference?

T obj;
T &r = obj;  // style-1
T* const p = &obj; // style-2

Both the style can be used for the same purpose. I always prefer the 1st style in the code and consider the later style as deprecated. However I still wonder if missed any use case where 2nd style is better ?

Edit: Not limiting to the above example, I talk in a broader sense,

void foo (T& r); // style-1
void foo (T* const p); // style-2

[I see from few of the answers that, style-2 allows to pass null.]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

咽泪装欢 2024-12-14 13:00:23

const 指针 (T* const) 可以是 NULL,这在向函数传递参数或从函数返回值时非常有用。功能。例如,如果您有一个搜索函数,您可能希望它在未找到任何内容时返回 NULL。如果它返回引用类型,则无法执行此操作。

A const pointer (T* const) can be NULL, and that can be useful when passing arguments to a function or when returning values from a function. For example, if you have a search function, you might want it to return NULL if it didn't find anything. You can't do that if it returns a reference type.

掩于岁月 2024-12-14 13:00:23

让我在这里冒险一下。由于您明确地说“const 指针”,我假设您不是在谈论函数参数甚至函数返回值。对于通过副本传递的函数参数,常量是一个不相关的实现细节:

void foo(T *);          // declaration

void foo(T * const pt)  // implementation,
{ /* ... */ }           // pt is const inside the body (who cares?)

因此,想到的唯一用例是您是否需要在自己的代码中的某个位置创建别名。在这种情况下,我总是更喜欢参考。比较:

for (auto it = box.begin(); it != box.end(); ++it)
{
  T & trinket = *it;     // nice
  T * const ptr = &*it;  // wtpf?

  // ...
}

自从您编辑问题以来:函数参数显然存在差异。

void foo(T &);
void bar(T *);

foo中,你保证有一个可用的、可变的对象引用;在 bar 中,您必须检查指针是否不为空(给您一个可选参数的概念)。从这个意义上说,T&T* 并没有真正的可比性。

Let me go out on a limb here. Since you explicitly say "const pointer", I'm assuming that you are not talking about function arguments or even function return values. For a function argument passed by copy, the constness is an irrelevant implementation detail:

void foo(T *);          // declaration

void foo(T * const pt)  // implementation,
{ /* ... */ }           // pt is const inside the body (who cares?)

Therefore, the only use case that comes to mind is if you need to make an alias somewhere inside your own code. In that case I'd always prefer the reference. Compare:

for (auto it = box.begin(); it != box.end(); ++it)
{
  T & trinket = *it;     // nice
  T * const ptr = &*it;  // wtpf?

  // ...
}

Since you edited your question: There is obviously a difference for function arguments.

void foo(T &);
void bar(T *);

In foo you are guaranteed to have a workable, mutable object reference; in bar you have to check whether the pointer is not null (giving you the notion of an optional argument). In that sense, T& and T* aren't really comparable.

后来的我们 2024-12-14 13:00:23

对于函数参数,我更喜欢指向引用的指针,因为我可以在调用站点知道该参数是一个输出参数。如果函数参数是 const 引用,那么我更喜欢引用。

另一个很大的区别是 r 不能为空,所以如果你需要它,那么你需要 style-2

For function arguments, I much prefer pointers to references, because I can tell at the call site that the parameter is an out parameter. If the function argument is a const reference, then I prefer references.

The other big difference is r can't be null, so if you need it to be, then you need style-2

很快妥协 2024-12-14 13:00:23

1)使用引用时,对象必须存储在变量中。例如,你不能这样做:

void funct (T &);
funct (T ("Calling constructor"));

必须这样做:

void funct (T &);
T obj ("Calling constructor");
funct (obj);

2)当使用引用时,你不能测试它的值(例如,检查它是否是常用的 NULL)

通过其他一切,这是同一件事。

我通常仅在以 true 变量作为参数调用函数很重要的情况下才使用它们。我可能很守旧,但在其他情况下指针方式对我来说更好。

1) When using reference, the object must be stored in variable. For example, you can't do this:

void funct (T &);
funct (T ("Calling constructor"));

Must do:

void funct (T &);
T obj ("Calling constructor");
funct (obj);

2) When using reference, you cannot test it's value (eg. check if it's NULL which is commonly used)

By everything else, it's the same thing.

I generally use them only if it's important that function is called with true variable as argument. I might be old fashioned, but pointer way is better for me in every other cases.

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