因为加入引用是为了支持operator overloading。这里有一个假设,如果没有引用,那么,用指针来operator overloading操作。
A operator +(const A *a, const A *_a);
那么使用的时候,&a + &b,这样看起来是不是很难受。
而引入引用的概念,既可以满足overload operator,也不失重载value和pointer的灵活性。而且引用还带来一个指针无法替代的特性: 引用临时对象。因为引用必须在定义的时候就赋值,以后无法更改。
作者:Meowz
链接:https://www.zhihu.com/question/34988367/answer/60656358
来源:知乎
著作权归作者所有,转载请联系作者获得授权。
————————————————————————————————————————————————————
Stroustrup: C++ Style and Technique FAQ
Why does C++ have both pointers and references?
C++ inherited pointers from C, so I couldn't remove them without causing serious compatibility problems. References are useful for several things, but the direct reason I introduced them in C++ was to support operator overloading.
作者:Milo Yip
链接:https://www.zhihu.com/question/34988367/answer/60715029
来源:知乎
著作权归作者所有,转载请联系作者获得授权。