Skip to content

Instantly share code, notes, and snippets.

@xaxxon
Last active February 3, 2016 08:47
Show Gist options
  • Save xaxxon/f20cb67d585766d1fbbb to your computer and use it in GitHub Desktop.
Save xaxxon/f20cb67d585766d1fbbb to your computer and use it in GitHub Desktop.
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()));
}
}
{
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