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

后端 未结 5 1846
猫巷女王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: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.

提交回复
热议问题