为什么要使用指针参数?

编程入门 行业动态 更新时间:2024-10-25 04:16:28
本文介绍了为什么要使用指针参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

可能的重复项: 为什么使用指针? 将可修改的参数传递给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.

更多推荐

为什么要使用指针参数?

本文发布于:2023-06-01 10:28:29,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/412995.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:指针   要使   参数

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!