forked from parallel101/course
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
81 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#include <memory> | ||
|
||
struct C { | ||
std::shared_ptr<C> m_child; | ||
std::shared_ptr<C> m_parent; | ||
}; | ||
|
||
int main() { | ||
auto parent = std::make_shared<C>(); | ||
auto child = std::make_shared<C>(); | ||
|
||
// 建立相互引用: | ||
parent->m_child = child; | ||
child->m_parent = parent; | ||
|
||
parent = nullptr; // parent 不会被释放!因为 child 还指向他! | ||
child = nullptr; // child 也不会被释放!因为 parent 还指向他! | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#include <memory> | ||
|
||
struct C { | ||
std::shared_ptr<C> m_child; | ||
std::weak_ptr<C> m_parent; | ||
}; | ||
|
||
int main() { | ||
auto parent = std::make_shared<C>(); | ||
auto child = std::make_shared<C>(); | ||
|
||
// 建立相互引用: | ||
parent->m_child = child; | ||
child->m_parent = parent; | ||
|
||
parent = nullptr; // parent 会被释放。因为 child 指向他的是 **弱引用** | ||
child = nullptr; // child 会被释放。因为指向 child 的 parent 已经释放了 | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#include <memory> | ||
|
||
struct C { | ||
std::shared_ptr<C> m_child; | ||
C *m_parent; | ||
}; | ||
|
||
int main() { | ||
auto parent = std::make_shared<C>(); | ||
auto child = std::make_shared<C>(); | ||
|
||
// 建立相互引用: | ||
parent->m_child = child; | ||
child->m_parent = parent.get(); | ||
|
||
parent = nullptr; // parent 会被释放。因为 child 指向他的是原始指针 | ||
child = nullptr; // child 会被释放。因为指向 child 的 parent 已经释放了 | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#include <memory> | ||
|
||
struct C { | ||
std::unique_ptr<C> m_child; | ||
C *m_parent; | ||
}; | ||
|
||
int main() { | ||
auto parent = std::make_unique<C>(); | ||
auto child = std::make_unique<C>(); | ||
|
||
// 建立相互引用: | ||
parent->m_child = std::move(child); // 移交 child 的所属权给 parent | ||
child->m_parent = parent.get(); | ||
|
||
parent = nullptr; // parent 会被释放。因为 child 指向他的是原始指针 | ||
// 此时 child 也已经被释放了,因为 child 完全隶属于 parent | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Binary file not shown.