C++之引用和指针的区别
面试时被问到的一个问题,回答得不够全面,算是查漏补缺。
简述
我一直都有一个疑问,为什么在C++中有了指针还要引用?在Stroustrup: C++ Style and Technique FAQ中给出了回复,原文如下:
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.
翻译:C++中指针继承自C语言,出于兼容的原因保留了下来。引用的用处很多,但是C++引入它最直接的原因是为了支持运算符重载。