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

相關文章
相關標籤/搜索