Item 28: Smart pointers.(More Effective C++)
2012-03-15 14:33
351 查看
When you use smart pointers in place of C++'s built-in pointers (i.e., dumb pointers), you gain control over the following aspects of pointer behavior:
Construction and destruction. You determine what happens when a smart pointer is created and destroyed. It is common to give smart pointers a default value of 0 to avoid the headaches associated with uninitialized pointers. Some smart pointers are made responsible for deleting the object they point to when the last smart pointer pointing to the object is destroyed. This can go a long way toward eliminating resource leaks.
Copying and assignment. You control what happens when a smart pointer is copied or is involved in an assignment. For some smart pointer types, the desired behavior is to automatically copy or make an assignment to what is pointed to, i.e., to perform a deep copy. For others, only the pointer itself should be copied or assigned. For still others, these operations should not be allowed at all. Regardless of what behavior you consider "right," the use of smart pointers lets you call the shots.
Dereferencing. What should happen when a client refers to the object pointed to by a smart pointer? You get to decide. You could, for example, use smart pointers to help implement the lazy fetching strategy outlined in Item 17.
Construction and destruction. You determine what happens when a smart pointer is created and destroyed. It is common to give smart pointers a default value of 0 to avoid the headaches associated with uninitialized pointers. Some smart pointers are made responsible for deleting the object they point to when the last smart pointer pointing to the object is destroyed. This can go a long way toward eliminating resource leaks.
Copying and assignment. You control what happens when a smart pointer is copied or is involved in an assignment. For some smart pointer types, the desired behavior is to automatically copy or make an assignment to what is pointed to, i.e., to perform a deep copy. For others, only the pointer itself should be copied or assigned. For still others, these operations should not be allowed at all. Regardless of what behavior you consider "right," the use of smart pointers lets you call the shots.
Dereferencing. What should happen when a client refers to the object pointed to by a smart pointer? You get to decide. You could, for example, use smart pointers to help implement the lazy fetching strategy outlined in Item 17.
相关文章推荐
- More Effective C++ 条款28 Smart Pointers(智能指针)
- [翻译] Effective C++, 3rd Edition, Item 17: 在 standalone statements(独立语句)中将 new 出来的 objects(对象)存入 smart pointers(智能指针)
- Item 1:Distinguish between pointers and references.(More Effective C++)
- Item 17: Store newed objects in smart pointers in standalone statements.(Effective C++)
- More Effective C++----(28)灵巧(smart)指针
- More Effective C++ 读书摘要(五、技巧2)Item28 - 29
- Effective C# Item28:避免强制类型转换
- More Effective C# Item2 : 恰到好处的定义约束
- [翻译] Effective C++, 3rd Edition, Item 28: 避免返回 object 内部构件的 "handles"(“句柄”)
- Item 10:Prevent resource leaks in constructors.(More Effective C++)
- 《more effective c++》Item M3:不要对数组使用多态
- More Effective C++ 条款28(上)
- more effective c++——Item M30 代理类(三)隐式类型转换与代理类
- More Effective C# Item 7. Do Not Create Generic Specialization on Base Classes or Interfaces
- More Effective C# Item7 : 不要为基类或者接口创建泛型的特殊实现
- More Effective c++ 28. Smart Pointer
- More Effective C++ 条款28(中)
- Item 4:Avoid gratuitous default constructors.(More Effective C++)
- Item 6:Distinguish between prefix and postfix forms of increment and decrement operators.(More Effective C++)
- Item 13:Catch exceptions by reference.(More Effective C++)