在 c++ 中引入引用的根本原因到底是什么?
从我最近的问题中发生的讨论来看(为什么c++ 引用被认为比指针更安全?),它在我的脑海中提出了另一个问题:在 c++ 中引入引用背后的基本原理到底是什么?
From the discussion that has happened in my recent question (Why is a c++ reference considered safer than a pointer?), it raises another question in my mind: What exactly was the rationale behind introducing references in c++?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
Stroustrup 的 C++ 的设计和演化 的第 3.7 节描述了在语言中引入引用。如果您对 C++ 任何功能背后的基本原理感兴趣,我强烈推荐这本书。
Section 3.7 of Stroustrup's Design and Evolution of C++ describes the introduction of references into the language. If you're interested in the rationale behind any feature of C++, I highly recommend this book.
您需要它们来进行运算符重载(当然,我们现在可以深入探讨“引入运算符重载的基本原理是什么?”)
您将如何输入
std::auto_ptr::operator*()
没有参考文献?或者std::vector::operator[]?You need them for operator overloading (of course we can now go down the rabbit hole of "what was the rationale for introducing operator overloading?")
How would you type
std::auto_ptr::operator*()
without references? Orstd::vector::operator[]
?引用隐式绑定到对象。当您考虑诸如绑定到临时变量或运算符重载之类的事情时,这具有很大的优势 - C++ 程序将充满 & 。和 *。当您考虑一下时,指针的基本用例实际上是引用的行为。此外,搞砸引用要困难得多——你自己不执行任何指针算术,不能自动从数组转换(一件可怕的事情),等等。
引用比指针更干净、更容易、更安全。
这很有趣,因为大多数其他语言没有像 C++ 那样的引用(别名),它们只有指针样式的引用。
References bind to objects implicitly. This has large advantages when you consider things like binding to temporaries or operator overloading- C++ programs would be full of & and *. When you think about it, the basic use case of a pointer is actually to behave of a reference. In addition, it's much harder to screw up references- you don't perform any pointer arithmetic yourself, can't automatically convert from arrays (a terrible thing), etc.
References are cleaner, easier, and safer than pointers.
It's interesting because most other languages don't have references like C++ has them (aliases), they just have pointer-style references.
如果代码获取变量的地址并将其传递给例程,则编译器无法知道该地址是否可能存储在某处并在被调用例程退出后很长时间(并且可能在变量不再存在后)使用。相比之下,如果代码传递给例程提供对变量的引用,则可以更好地保证该引用仅在例程运行时使用。一旦该例程返回,该引用将不再被使用。
由于 C++ 允许代码获取引用的地址,事情最终变得有点“糟糕”。提供此功能是为了与需要指针而不是引用的旧例程兼容。如果一个引用被传递给一个获取其地址并将其存储在某个地方的例程,那么所有的赌注都会被取消。另一方面,如果作为一项政策,禁止以任何可能持久的方式使用引用的地址,那么人们就可以很好地获得引用提供的保证。
If code takes the address of a variable and passes it to a routine, the compiler has no way of knowing whether that address might get stored someplace and used long after the called routine has exited, and possibly after the variable has ceased to exist. By contrast, if code passes give a routine a reference to a variable, it has somewhat more assurance that the reference will only be used while that routine is running. Once that routine returns, the reference will no longer be used.
Things end up getting a little 'broken' by the fact that C++ allows code to take the address of a reference. This ability was provided to allow compatibility with older routines which expected pointers rather than references. If a reference is passed to a routine which takes its address and stores it someplace, all bets are off. On the other hand, if as a matter of policy one forbids using the address of a reference in any way that might be persisted, one can pretty well gain the assurances that references provide.
允许操作员重载。他们希望运算符对于对象和指针都是可重载的,因此他们需要一种通过指针以外的东西来引用对象的方法。因此引入了参考。它在《C++的设计与演化》中。
To allow for operator overloading. They wanted operators to be overloadable both for objects and pointers, so they needed a way to refer to an object by something other than a pointer. Hence the reference was introduce. It is in "The Design and Evolution of C++".