Skip to content

Commit 82ebebb

Browse files
committed
book: Chapter 5 since markers (smart pointers)
Add 'since C++11' markers to shared_ptr/unique_ptr/weak_ptr. The RAII intro already strongly motivates why smart pointers exist.
1 parent ca4daaa commit 82ebebb

2 files changed

Lines changed: 12 additions & 0 deletions

File tree

book/en-us/05-pointers.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ These smart pointers include `std::shared_ptr`/`std::unique_ptr`/`std::weak_ptr`
2626
2727
## 5.2 `std::shared_ptr`
2828

29+
*(since C++11)*
30+
2931
`std::shared_ptr` is a smart pointer that records how many `shared_ptr` points to an object, eliminating to call `delete`, which automatically deletes the object when the reference count becomes zero.
3032

3133
But not enough, because using `std::shared_ptr` still needs to be called with `new`, which makes the code a certain degree of asymmetry.
@@ -82,6 +84,8 @@ std::cout << "pointer3.use_count() = "
8284

8385
## 5.3 `std::unique_ptr`
8486

87+
*(since C++11)*
88+
8589
`std::unique_ptr` is an exclusive smart pointer that prohibits other smart pointers from sharing the same object by copy construction or copy assignment, thus avoiding errors due to repeated destruction or freeing:
8690

8791
```cpp
@@ -148,6 +152,8 @@ int main() {
148152
149153
## 5.4 `std::weak_ptr`
150154
155+
*(since C++11)*
156+
151157
If you think about `std::shared_ptr` carefully, you will still find that there is still a problem that resources cannot be released. Look at the following example:
152158
153159
```cpp

book/zh-cn/05-pointers.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ order: 5
2727
2828
## 5.2 `std::shared_ptr`
2929

30+
*(C++11)*
31+
3032
`std::shared_ptr` 是一种智能指针,它能够记录多少个 `shared_ptr` 共同指向一个对象,从而消除显式的调用
3133
`delete`,当引用计数变为零的时候就会将对象自动删除。
3234

@@ -80,6 +82,8 @@ std::cout << "pointer3.use_count() = "
8082

8183
## 5.3 `std::unique_ptr`
8284

85+
*(C++11)*
86+
8387
`std::unique_ptr` 是一种独占的智能指针,它禁止其他智能指针经由拷贝构造或拷贝赋值的方式与其共享同一个对象,从而避免重复析构或释放带来的错误:
8488

8589
```cpp
@@ -139,6 +143,8 @@ int main() {
139143
140144
## 5.4 `std::weak_ptr`
141145
146+
*(C++11)*
147+
142148
如果你仔细思考 `std::shared_ptr` 就会发现依然存在着资源无法释放的问题。看下面这个例子:
143149
144150
```cpp

0 commit comments

Comments
 (0)