栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > C++面试题库

Different between C++ pointers and references?

Different between C++ pointers and references?

Because a reference must refer to an object, C++ requires that references be initialized.
Notice that there is no such thing as a null reference.

Another important difference between pointers and references is that pointers may be reassigned to refer to different objects. A reference, however, always refers to the object with which it is initialized

In general, you should use a pointer whenever you need to take into account the possibility that there’s nothing to refer to (in which case you can set the pointer to null) or whenever you need to be able to refer to different things at different times (in which case you can change where the pointer points). You should use a reference whenever you know there will always be an object to refer to and you also know that once you’re referring to that object, you’ll never want to refer to anything else

Other occassions: you’ll almost always want operator[] to return a reference.

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/265775.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号