References, then, are the feature of choice when you know you have something to refer to, when you'll never want to refer to anything else, and when implementing operators whose syntactic requirements make the use of pointers undesirable. In all other cases, stick with pointers.
Item 1:Distinguish between pointers and references.(More Effective C++)
转载
First, recognize that there is no such thing as a null reference. A reference must always refer to some object. As a result, if you have a variable whose purpose is to refer to another object, but it is possible that there might not be an object to refer to, you should make the variable a pointer, because then you can set it to null. On the other hand, if the variable must always refer to an object, i.e., if your design does not allow for the possibility that the variable is null, you should probably make the variable a reference.
本文章为转载内容,我们尊重原作者对文章享有的著作权。如有内容错误或侵权问题,欢迎原作者联系我们进行内容更正或删除文章。
上一篇:Item 3:Never treat arrays polymorphically.(More Effective C++)
下一篇:Item.55: Familiarize yourself with Boost.(Effective C++)

提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
More Effective C++:异常
强制性:异常不能被忽略,这与传统的错误代码不同,后者可能被忽略。安全性:异常确保了错误状态被正确处理,而非让
c++ 抛出异常 析构函数 构造函数 -
More Effective C++:技巧
虚拟构造函数并不是一个真正意义上的构造函数,而是一个设计模式。这种模式允许根据输入数据动态创建不同子类的对象。通常,这种
c++ 构造函数 Graphic 析构函数 -
《More Effective C++ 》笔记
条款10 防止构造函数里的资源泄露条款20 协助编译器实现返回值优化条款22 考虑使用op=来取代单独的op运算符条款26 限制类对象的个数条款27 要求或禁止对象分配在堆上
构造函数 类对象 编译器 运算符 javascript