为什么要使用指针参数? [英] Why have pointer parameters?
问题描述
可能的重复项:
为什么使用指针?
将可修改的参数传递给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.
推荐答案
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.
这篇关于为什么要使用指针参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!