在引用 (T&) 和 const 指针 (T* const) 之间进行选择
是否有任何合理用例,应该使用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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
const
指针 (T* const
) 可以是NULL
,这在向函数传递参数或从函数返回值时非常有用。功能。例如,如果您有一个搜索函数,您可能希望它在未找到任何内容时返回NULL
。如果它返回引用类型,则无法执行此操作。A
const
pointer (T* const
) can beNULL
, 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 returnNULL
if it didn't find anything. You can't do that if it returns a reference type.让我在这里冒险一下。由于您明确地说“const 指针”,我假设您不是在谈论函数参数甚至函数返回值。对于通过副本传递的函数参数,常量是一个不相关的实现细节:
因此,想到的唯一用例是您是否需要在自己的代码中的某个位置创建别名。在这种情况下,我总是更喜欢参考。比较:
自从您编辑问题以来:函数参数显然存在差异。
在
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:
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:
Since you edited your question: There is obviously a difference for function arguments.
In
foo
you are guaranteed to have a workable, mutable object reference; inbar
you have to check whether the pointer is not null (giving you the notion of an optional argument). In that sense,T&
andT*
aren't really comparable.对于函数参数,我更喜欢指向引用的指针,因为我可以在调用站点知道该参数是一个输出参数。如果函数参数是 const 引用,那么我更喜欢引用。
另一个很大的区别是
r
不能为空,所以如果你需要它,那么你需要 style-2For 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-21)使用引用时,对象必须存储在变量中。例如,你不能这样做:
必须这样做:
2)当使用引用时,你不能测试它的值(例如,检查它是否是常用的 NULL)
通过其他一切,这是同一件事。
我通常仅在以 true 变量作为参数调用函数很重要的情况下才使用它们。我可能很守旧,但在其他情况下指针方式对我来说更好。
1) When using reference, the object must be stored in variable. For example, you can't do this:
Must do:
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.