通过引用传递与通过指针传递——优点?
可能的重复:
何时首选传递指针在 C++ 中按引用传递?
路过有好处吗C++ 中通过引用传递指针?
如何将对象传递给 C++ 中的函数?
大家好, 我正在努力开发一个大型面向对象的 C++ 应用程序框架,作为我的化学工程研究生研究的一部分。
我发现我的许多函数都采用指向自定义对象或 STL 对象的指针。我发现就编写代码而言,这使得访问其中存储的函数或变量变得更加困难。
但是,除了简单性之外,通过引用传递与通过指针传递相比还有什么优点/缺点吗?
如果其中一种比另一种有优势,我可能会考虑重构我的代码以统一使用任何最佳方法。如果没有,我仍然可以重构以一致地使用引用传递以提高可读性(即不必取消引用)
再次,我想做出最佳决定,因为我的框架已经有 40 多个文件大,所以我想实现为统一结构尽我所能,尽早采取任何最佳方法。
提前致谢!
Possible Duplicates:
When pass-by-pointer is preferred to pass-by-reference in C++?
Are there benefits of passing by pointer over passing by reference in C++?
How to pass objects to functions in C++?
Hi all,
I'm working to develop a large object oriented c++ application framework as part of my chemical engineering graduate research.
I find that many of my functions take pointers to custom objects or STL objects. I find that in terms of writing code, this makes it harder to access functions or variables stored within.
Aside from simplicity, though, is there any advantages/disadvantages to passing by reference v. passing by pointer?
If there is an advantage to one over the other, I'll probably look to refactor my code to uniformly use whatever approach is optimal. If there isn't I may still refactor to consistently use pass by reference for readability (i.e. not having to dereference)
Again I want to make the best decision as my framework is already 40+ files large, so I want to implement as uniform structure as I can, as early as I can, and with whatever the optimal method is.
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
主要区别在于引用不能为 NULL(至少在没有恶意编程的情况下不能为 NULL)。对于语法糖以及需要参数时,我会通过引用传递。如果我遇到参数是可选的情况,我会通过指针传递。
总有例外。如果它符合当前的风格,或者由于我必须用它做一些事情,那么将它作为指针可能会更方便。
The main difference is that a reference cannot be NULL (at least not without some malicious programming). For syntactical sugar and when an argument is required, I'd pass by reference. If I had a situation where the argument were optional, I'd pass by pointer.
There are always exceptions. If it conforms to the current style, or due to things I have to do with it, it may be more convenient to have it as a pointer.
更喜欢通过引用传递。如果没有其他原因,我会这样做,因为引用根本不能为 NULL。它结束了这么多愚蠢的错误、辩论、合同检查、内存责任问题等等......
Prefer pass by reference. If for nothing else I do it because a reference simply can't be
NULL
. It puts an end to so many stupid bugs, debates, contract checks, memory responsibility questions, etc ...仅供参考,今天早些时候有人以稍微不同的方式询问了这个问题:
在 C++ 中何时通过引用传递以及何时通过指针传递?
规范的建议是“通过引用传递给 const,除非有充分的理由进行其他选择”。
FYI this was asked in a slightly diff way earlier today:
When to pass by reference and when to pass by pointer in C++?
Canonical advice is "pass by ref to const unless a v good reason for another choice".
如果您选择指针还是引用,请使用引用。它们具有指针的所有优点,而没有取消引用未初始化指针的危险。
并且不要太担心尽早实现统一的方法 - 在这种情况下,即使代码库很大,在两者之间来回(一次一个函数)实际上并不困难。
If your choice is pointers vs. references, use references. They have all the advantages of pointers without the danger of dereferencing an uninitialized pointer.
And don't worry too much about implementing a uniform approach early-- in this case it really isn't difficult to go back and forth between the two (one function at a time) even when the code base is large.
就我个人而言,我喜欢传递指针,因为它在我的代码中提供了一定程度的一致性。我自动知道我的对象 Foo 是一个指针。这同样适用于参考,所以一定要选择一个并坚持使用。
Personally I like passing by pointer because it offers some level of consistency in my code. I automatically know that my object Foo is a pointer. The same would apply with a reference so be sure to pick one and stick with it.
虽然我喜欢引用,但在一些常见情况下您不能使用它们:
在我看来,混合引用和指针非常难看,所以我更喜欢指针。
While I like references, there are a few common cases where you can't use them:
In my view, having a mix of references and pointers is pretty ugly, so I prefer pointers.