mirror of
https://github.com/CnTransGroup/EffectiveModernCppChinese.git
synced 2024-12-27 13:21:07 +08:00
item18: smart pointer
This commit is contained in:
parent
c5ceccc449
commit
039956c279
19
4.SmartPointers/item18.md
Normal file
19
4.SmartPointers/item18.md
Normal file
@ -0,0 +1,19 @@
|
||||
# CHAPTER 4 Smart Pointers
|
||||
|
||||
诗人和歌曲作家喜欢爱。有时候喜欢计数。很少情况下两者兼有。受伊丽莎白·巴雷特·勃朗宁(Elizabeth Barrett Browning)对爱和数的不同看法的启发(“我怎么爱你?”让我数一数。”)和保罗·西蒙(Paul Simon)(“离开你的爱人必须有50种方法。”),我们可以试着枚举一些为什么原始指针很难被爱的原因:
|
||||
|
||||
1. 它的声明不能指示所指到底是单个对象还是数组
|
||||
2. 它的声明没有告诉你用完后是否应该销毁它,即指针是否拥有所指之物
|
||||
3. 如果你决定你应该销毁对象所指,没人告诉你该用delete还是其他析构机制(比如将指针传给专门的销毁函数)
|
||||
4. 如果你发现该用delete。 原因1说了不知道是delete单个对象还是delete数组。如果用错了结果是未定义的
|
||||
5. 假设你确定了指针所指,知道销毁机制,也很难确定你在所有执行路径上都执行了销毁操作(包括异常产生后的路径)。少一条路径就会产生资源泄漏,销毁多次还会导致未定义行为
|
||||
6. 一般来说没有办法告诉你指针是否变成了悬空指针(dangling pointers),即内存中不再存在指针所指之物。悬空指针会在对象销毁后仍然指向它们。
|
||||
|
||||
原始指针是强大的工具,当然,另一方面几十年的经验证明,只要注意力稍有疏忽,这个强大的工具就会攻击它的主人。
|
||||
|
||||
智能指针是解决这些问题的一种办法。智能指针包裹原始指针,它们的行为看起来像被包裹的原始指针,但避免了原始指针的很多陷阱。你应该更倾向于只能指针而不是原始指针。几乎原始指针能做的所有事情智能指针都能做,而且出错的机会更少。
|
||||
|
||||
|
||||
|
||||
## Item 18:对于独占资源使用std::unique_ptr
|
||||
条款十八:对于独占资源使用std::unique_ptr
|
40
README.md
40
README.md
@ -1,5 +1,5 @@
|
||||
# 《Effective Modern C++ 》翻译
|
||||
![bookLogo](https://github.com/racaljk/EffectiveModernCppChinese/blob/master/x.public/1.png?raw=true)
|
||||
![bookLogo](https://github.com/kelthuzadx/EffectiveModernCppChinese/blob/master/x.public/1.png?raw=true)
|
||||
[![Backers on Open Collective](https://opencollective.com/EffectiveModernCppChinese/backers/badge.svg)](#backers)
|
||||
[![Sponsors on Open Collective](https://opencollective.com/EffectiveModernCppChinese/sponsors/badge.svg)](#sponsors)
|
||||
|
||||
@ -11,27 +11,27 @@
|
||||
|
||||
# 目录
|
||||
1. 类型推导
|
||||
1. [Item 1:理解模板类型推导](https://github.com/racaljk/EffectiveModernCppChinese/blob/master/1.DeducingTypes/item1.md) __已修订__
|
||||
2. [Item 2:理解auto类型推导](https://github.com/racaljk/EffectiveModernCppChinese/blob/master/1.DeducingTypes/item2.md)
|
||||
3. [Item 3:理解decltype](https://github.com/racaljk/EffectiveModernCppChinese/blob/master/1.DeducingTypes/item3.md)
|
||||
3. [Item 4:学会查看类型推导结果](https://github.com/racaljk/EffectiveModernCppChinese/blob/master/1.DeducingTypes/item4.md)
|
||||
1. [Item 1:理解模板类型推导](https://github.com/kelthuzadx/EffectiveModernCppChinese/blob/master/1.DeducingTypes/item1.md) __已修订__
|
||||
2. [Item 2:理解auto类型推导](https://github.com/kelthuzadx/EffectiveModernCppChinese/blob/master/1.DeducingTypes/item2.md)
|
||||
3. [Item 3:理解decltype](https://github.com/kelthuzadx/EffectiveModernCppChinese/blob/master/1.DeducingTypes/item3.md)
|
||||
3. [Item 4:学会查看类型推导结果](https://github.com/kelthuzadx/EffectiveModernCppChinese/blob/master/1.DeducingTypes/item4.md)
|
||||
2. auto
|
||||
1. [Item 5:优先考虑auto而非显式类型声明](https://github.com/racaljk/EffectiveModernCppChinese/blob/master/2.auto/item5.md)
|
||||
2. [Item 6:auto推导若非己愿,使用显式类型初始化惯用法](https://github.com/racaljk/EffectiveModernCppChinese/blob/master/2.auto/item6.md)
|
||||
1. [Item 5:优先考虑auto而非显式类型声明](https://github.com/kelthuzadx/EffectiveModernCppChinese/blob/master/2.auto/item5.md)
|
||||
2. [Item 6:auto推导若非己愿,使用显式类型初始化惯用法](https://github.com/kelthuzadx/EffectiveModernCppChinese/blob/master/2.auto/item6.md)
|
||||
3. 移步现代C++
|
||||
1. [Item 7:区别使用()和{}创建对象](https://github.com/racaljk/EffectiveModernCppChinese/blob/master/3.MovingToModernCpp/item7.md)
|
||||
2. [Item 8:优先考虑nullptr而非0和NULL](https://github.com/racaljk/EffectiveModernCppChinese/blob/master/3.MovingToModernCpp/item8.md)
|
||||
3. [Item 9:优先考虑别名声明而非typedefs](https://github.com/racaljk/EffectiveModernCppChinese/blob/master/3.MovingToModernCpp/item9.md)
|
||||
4. [Item 10:优先考虑限域枚举而非未限域枚举](https://github.com/racaljk/EffectiveModernCppChinese/blob/master/3.MovingToModernCpp/item10.md) __已修订__
|
||||
5. [Item 11:优先考虑使用deleted函数而非使用未定义的私有声明](https://github.com/racaljk/EffectiveModernCppChinese/blob/master/3.MovingToModernCpp/item11.md)
|
||||
6. [Item 12:使用override声明重载函数](https://github.com/racaljk/EffectiveModernCppChinese/blob/master/3.MovingToModernCpp/item12.md)
|
||||
7. [Item 13:优先考虑const_iterator而非iterator](https://github.com/racaljk/EffectiveModernCppChinese/blob/master/3.MovingToModernCpp/item13.md)
|
||||
8. [Item 14:如果函数不抛出异常请使用noexcept](https://github.com/racaljk/EffectiveModernCppChinese/blob/master/3.MovingToModernCpp/item14.md)
|
||||
9. [Item 15:尽可能的使用constexpr](https://github.com/racaljk/EffectiveModernCppChinese/blob/master/3.MovingToModernCpp/item15.md)
|
||||
10. [Item 16:让const成员函数线程安全](https://github.com/racaljk/EffectiveModernCppChinese/blob/master/3.MovingToModernCpp/item16.md) __由 @windski贡献__
|
||||
11. [Item 17:理解特殊成员函数函数的生成](https://github.com/racaljk/EffectiveModernCppChinese/blob/master/3.MovingToModernCpp/item17.md) __更新中__
|
||||
1. [Item 7:区别使用()和{}创建对象](https://github.com/kelthuzadx/EffectiveModernCppChinese/blob/master/3.MovingToModernCpp/item7.md)
|
||||
2. [Item 8:优先考虑nullptr而非0和NULL](https://github.com/kelthuzadx/EffectiveModernCppChinese/blob/master/3.MovingToModernCpp/item8.md)
|
||||
3. [Item 9:优先考虑别名声明而非typedefs](https://github.com/kelthuzadx/EffectiveModernCppChinese/blob/master/3.MovingToModernCpp/item9.md)
|
||||
4. [Item 10:优先考虑限域枚举而非未限域枚举](https://github.com/kelthuzadx/EffectiveModernCppChinese/blob/master/3.MovingToModernCpp/item10.md) __已修订__
|
||||
5. [Item 11:优先考虑使用deleted函数而非使用未定义的私有声明](https://github.com/kelthuzadx/EffectiveModernCppChinese/blob/master/3.MovingToModernCpp/item11.md)
|
||||
6. [Item 12:使用override声明重载函数](https://github.com/kelthuzadx/EffectiveModernCppChinese/blob/master/3.MovingToModernCpp/item12.md)
|
||||
7. [Item 13:优先考虑const_iterator而非iterator](https://github.com/kelthuzadx/EffectiveModernCppChinese/blob/master/3.MovingToModernCpp/item13.md)
|
||||
8. [Item 14:如果函数不抛出异常请使用noexcept](https://github.com/kelthuzadx/EffectiveModernCppChinese/blob/master/3.MovingToModernCpp/item14.md)
|
||||
9. [Item 15:尽可能的使用constexpr](https://github.com/kelthuzadx/EffectiveModernCppChinese/blob/master/3.MovingToModernCpp/item15.md)
|
||||
10. [Item 16:让const成员函数线程安全](https://github.com/kelthuzadx/EffectiveModernCppChinese/blob/master/3.MovingToModernCpp/item16.md) __由 @windski贡献__
|
||||
11. [Item 17:理解特殊成员函数函数的生成](https://github.com/kelthuzadx/EffectiveModernCppChinese/blob/master/3.MovingToModernCpp/item17.md)
|
||||
4. 智能指针
|
||||
1. Item 18:对于占有性资源使用std::unique_ptr
|
||||
1. [Item 18:对于独占资源使用std::unique_ptr](https://github.com/kelthuzadx/EffectiveModernCppChinese/blob/master/4.SmartPointers/item18.md) __更新中__
|
||||
2. Item 19:对于共享性资源使用std::shared_ptr
|
||||
3. Item 20:对于类似于std::shared_ptr的指针使用std::weak_ptr可能造成悬置
|
||||
4. Item 21:优先考虑使用std::make_unique和std::make_shared而非new
|
||||
@ -64,7 +64,7 @@
|
||||
## 贡献者
|
||||
|
||||
感谢所有参与翻译/勘误/建议的贡献者们~
|
||||
<a href="https://github.com/racaljk/EffectiveModernCppChinese/graphs/contributors"><img src="https://opencollective.com/EffectiveModernCppChinese/contributors.svg?width=890&button=false" /></a>
|
||||
<a href="https://github.com/kelthuzadx/EffectiveModernCppChinese/graphs/contributors"><img src="https://opencollective.com/EffectiveModernCppChinese/contributors.svg?width=890&button=false" /></a>
|
||||
|
||||
|
||||
## 赞助翻译
|
||||
|
Loading…
Reference in New Issue
Block a user