可能的重复项: 为什么使用指针? 将可修改的参数传递给c ++函数
Possible Duplicates: Why use pointers? Passing a modifiable parameter to c++ function
我为什么要有指针参数?我看到的唯一原因是小型函数试图减少混乱并减小内存占用.
Why would I want to have pointer parameters? The only reason I can see is for small functions to attempt to reduce confusion and a smaller memory footprint.
推荐答案在将可修改的参数传递给c ++函数我回答了何时使用引用而不是指针.
In Passing a modifiable parameter to c++ function I answered when to use a reference instead of a pointer.
相反,当满足以下任一条件时,建议使用指向引用的指针:
Conversely, prefer a pointer to a reference when any of the following are true:
- 可以为空
- 可以更改(指向其他内容)
- 必须删除
当所指向的对象应该发生突变时,有些人也喜欢使用指针,这表示const引用和非const引用之间的区别对读者来说并不明显.
Some people also prefer a pointer when the thing that's being pointed at should be mutated, saying that the difference between a const reference and a non-const reference isn't obvious enough to the reader.
更多推荐
为什么要使用指针参数?
发布评论