Item 1:Distinguish between pointers and references.(More Effective C++)
2011-06-02 15:44
796 查看
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. 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 6:Distinguish between prefix and postfix forms of increment and decrement operators.(More Effective C++)
- Item 1: distinguish between pointers and references
- Effective C# Item 6: Distinguish Between Value Types and Reference Types
- Effective C# Item 20: Distinguish Between Implementing Interfaces and Overriding Virtual Functions
- more effective c++:the difference between pointer and reference
- Distinguish between pointers and references in C++
- Item 34: Differentiate between inheritance of interface and inheritance of implementation(Effective C++)
- Item 28: Smart pointers.(More Effective C++)
- Effective C++ Item 34 Differentiate between inheritance of interface and inheritance of implementation
- Item 6: Distinguish Between Value Types and Reference Types
- 读书笔记《More Effective C++》条款1:仔细区别pointers和references
- More Effective C++ 条款1 仔细区别pointers和references
- Item 8:Understand the different meanings of new and delete.(More Effective C++)
- Item 18: Distinguish Between Value Types and Reference Types(Effective C#)
- Item31: Minimize compilation dependencies between files.(Effective C++)
- Item 25: Virtualizing constructors and non-member functions.(More Effective C++)
- more effective c++笔记1-----Item M2:尽 量使用C++风格的类型转换
- Item 30: Proxy classes.(More Effective C++)
- More Effective C++ 读书摘要(五、技巧2)Item28 - 29
- 《More Effective C++》 Item M1:指针与引用的区别