Last active
February 3, 2016 08:47
-
-
Save xaxxon/f20cb67d585766d1fbbb to your computer and use it in GitHub Desktop.
This file contains hidden or 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
struct C (and D and E...) { | |
// same as B | |
} | |
class B : enable_shared_from_this<B> { | |
friend class A; // C has friend class B, etc.. | |
B(){} | |
shared_ptr<A> parent_a; | |
public: | |
B(shared_ptr<A> a) { | |
parent_a(a); | |
} | |
std::shared_ptr<C> make_c(){ | |
return shared_ptr<C>(new C(shared_from_this())); | |
} | |
} | |
class A : enable_shared_from_this<A> { | |
private: | |
A(){} | |
public: | |
static shared_ptr<A> create_a(){ | |
return shared_ptr<A>(new A()); | |
} | |
std::shared_ptr<B> make_b(){ | |
return shared_ptr<B>(new B(shared_from_this())); | |
} | |
} | |
This file contains hidden or 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
{ | |
shared_ptr<B> b; | |
{ | |
auto a = A::create_a(); | |
b = a->create_b(); | |
} | |
// a should still be around | |
} | |
// b then a should be destroyed |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment