何时使用引用与指针 [英] When to use References vs. Pointers

查看:115
本文介绍了何时使用引用与指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我理解指针与引用的语法和一般语义,我不能决定是什么时候在API中使用引用或指针更合适?



当然有些情况需要一个或另一个( operator ++ 需要引用参数),但是一般来说,我发现我更喜欢使用指针),因为语法清楚地表明变量被破坏性地传递。



Eg在下面的代码中:

  void add_one(int& n){n + = 1; } 
void add_one(int * const n){* n + = 1; }
int main(){
int a = 0;
add_one(a); //不清楚可以修改
add_one(& a); // a显然是破坏性地传递
}

)很明显发生了什么,所以对于API等,清晰度是一个很大的问题是指针不比引用更合适吗?这是否意味着引用应该只在必要时使用(例如 operator ++ )?



除了允许NULL值外,还有其他任何性能问题吗?

和处理原始数组,似乎选择归结于个人喜好。我接受了以下答案,引用 Google的C ++风格指南,因为它们提供了引用



编辑:



由于需要额外的工作来清理不应该为NULL的指针参数(例如 add_one(0)将调用指针版本并在运行时断开),从可维护性角度来看是有意义的,

解决方案

使用引用,无论你在哪里,你都可以使用引用。



$ p

避免使用指针。



因此,经验法则是只有在没有其他选择的情况下才使用指针。



例如,当函数在某些情况下返回nullptr并返回指向某个对象的指针时,它是一个有效的选项。也就是说,一个更好的选择是使用类似 boost :: optional



另一个例子是使用指向原始内存的指针来进行特定的内存操作。这应该被隐藏和本地化在非常狭窄的代码部分,以帮助限制整个代码库的危险部分。



在您的示例中,使用指针作为参数没有意义,因为:


  1. 如果您提供 nullptr 作为参数,您将进入undefined-behavior-land;

  2. 不允许(不容易发现技巧)1的问题。

  3. 引用属性版本更容易理解的用户:你必须提供一个有效的对象,而不是可以为null。

如果函数的行为必须使用或不使用给定对象,那么使用指针作为属性建议你可以传递 nullptr 作为参数,这是函数的罚款。这是用户和实现之间的合同。


I understand the syntax and general semantics of pointers versus references, what I can't decide is when is it more-or-less appropriate to use references or pointers in an API?

Naturally some situations need one or the other (operator++ needs a reference argument), but in general I'm finding I prefer to use pointers (and const pointers) as the syntax is clear that the variables are being passed destructively.

E.g. in the following code:

void add_one(int& n) { n += 1; }
void add_one(int* const n) { *n += 1; }
int main() {
  int a = 0;
  add_one(a); // not clear that a may be modified
  add_one(&a); // a is clearly being passed destructively
}

With the pointer, it's always (more) obvious what's going on, so for APIs and the like where clarity is a big concern are pointers not more appropriate than references? Does that mean references should only be used when necessary (e.g. operator++)? Are there any performance concerns with one or the other?

EDIT (OUTDATED):

Besides allowing NULL values and dealing with raw arrays, it seems the choice comes down to personal preference. I've accepted the answer below that references Google's C++ Style Guide, as they present the view that "References can be confusing, as they have value syntax but pointer semantics.".

EDIT:

Due to the additional work required to sanitise pointer arguments that should not be NULL (e.g. add_one(0) will call the pointer version and break during runtime), it makes sense from a maintainability perspective to use references where an object MUST be present, though it is a shame to lose the syntactic clarity.

解决方案

Use reference wherever you can, pointers wherever you must.

Avoid pointers until you can't.

The reason is that pointers make things harder to follow/read, less safe and far more dangerous manipulations than any other constructs.

So the rule of thumb is to use pointers only if there is no other choice.

For example, returning a pointer to an object is a valid option when the function can return nullptr in some cases and it is assumed it will. That said, a better option would be to use something similar to boost::optional.

Another example is to use pointers to raw memory for specific memory manipulations. That should be hidden and localized in very narrow parts of the code, to help limit the dangerous parts of the whole code base.

In your example, there is no point in using a pointer as argument because:

  1. if you provide nullptr as the argument, you're going in undefined-behaviour-land;
  2. the reference attribute version doesn't allow (without easy to spot tricks) the problem with 1.
  3. the reference attribute version is simpler to understand for the user: you have to provide a valid object, not something that could be null.

If the behaviour of the function would have to work with or without a given object, then using a pointer as attribute suggests that you can pass nullptr as the argument and it is fine for the function. That's kind of a contract between the user and the implementation.

这篇关于何时使用引用与指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆