C++为何要同时支持指针和引用

由于加入引用是为了支持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
来源:知乎
著做权归做者全部,转载请联系做者得到受权。对象

————————————————————————————————————————————————————ip

Stroustrup: C++ Style and Technique FAQrem

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
来源:知乎
著做权归做者全部,转载请联系做者得到受权。get

相关文章
相关标签/搜索