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
来源:知乎
著作权归作者所有,转载请联系作者获得授权。

————————————————————————————————————————————————————

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
来源:知乎
著作权归作者所有,转载请联系作者获得授权。

转载于:https://my.oschina.net/u/566896/blog/838959

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值