What exactly was the rationale behind introducing references in c++?

后端 未结 5 1817
猫巷女王i
猫巷女王i 2021-01-03 17:26

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 wa

相关标签:
5条回答
  • 2021-01-03 18:05

    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.

    References were introduced primarily to support operator overloading. Doug McIlroy recalls that once I was explaining some problems with a precursor to the current operator overloading scheme to him. He used the word reference with the startling effect that I muttered "Thank you," and left his office to reappear the next day with the current scheme essentially complete. Doug had reminded me of Algol68.

    C passes every function argument by value, and where passing an object by value would be inefficient or inappropriate the user can pass a pointer. This strategy doesn't work where operator overloading is used. In that case, notational convenience is essential because users cannot be expected to insert address-of operators if the objects are large. For example:

    a = b - c;
    

    is acceptable (that is, conventional) notation, but

    a = &b - &c;
    

    is not. Anyway, &b - &c already has a meaning in C, and I didn't want to change that.

    It is not possible to change what a reference refers to after initialization. That is, once a C++ reference is initialized, it cannot be re-bound. I had in the past been bitten by Algol68 references where r1 = r2 can either assign through r1 to the object referred to or assign a new reference value to r1 (re-binding r1) depending on the type of r2. I wanted to avoid such problems in C++.

    0 讨论(0)
  • 2021-01-03 18:05

    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++".

    0 讨论(0)
  • 2021-01-03 18:17

    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? Or std::vector::operator[]?

    0 讨论(0)
  • 2021-01-03 18:21

    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.

    0 讨论(0)
  • 2021-01-03 18:24

    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.

    0 讨论(0)
提交回复
热议问题