mirror of
https://github.com/isocpp/CppCoreGuidelines.git
synced 2025-12-17 12:44:42 +03:00
add example for R.24 (#1127)
* add example for R.24 taken from https://cpppatterns.com/patterns/weak-reference.html, where it's published under CC0 Public Domain Dedication * improve example for R.24
This commit is contained in:
@@ -9151,7 +9151,35 @@ be able to destroy a cyclic structure.
|
|||||||
|
|
||||||
##### Example
|
##### Example
|
||||||
|
|
||||||
???
|
#include <memory>
|
||||||
|
|
||||||
|
class bar;
|
||||||
|
|
||||||
|
class foo
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
explicit foo(const std::shared_ptr<bar>& forward_reference)
|
||||||
|
: forward_reference_(forward_reference)
|
||||||
|
{ }
|
||||||
|
private:
|
||||||
|
std::shared_ptr<bar> forward_reference_;
|
||||||
|
};
|
||||||
|
|
||||||
|
class bar
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
explicit bar(const std::weak_ptr<foo>& back_reference)
|
||||||
|
: back_reference_(back_reference)
|
||||||
|
{ }
|
||||||
|
void do_something()
|
||||||
|
{
|
||||||
|
if (auto shared_back_reference = back_reference_.lock()) {
|
||||||
|
// Use *shared_back_reference
|
||||||
|
}
|
||||||
|
}
|
||||||
|
private:
|
||||||
|
std::weak_ptr<foo> back_reference_;
|
||||||
|
};
|
||||||
|
|
||||||
##### Note
|
##### Note
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user