fork download
  1. #include <memory>
  2.  
  3. struct B : public std::enable_shared_from_this<B> {
  4. };
  5.  
  6. struct A : public std::enable_shared_from_this<A> {
  7. A()
  8. : b_()
  9. {}
  10.  
  11. std::shared_ptr<B> b_;
  12. void SomeFunction();
  13. };
  14.  
  15. void A::SomeFunction() {
  16. auto a_copy = shared_from_this();
  17. }
  18.  
  19. int main()
  20. {
  21. A a;
  22. a.SomeFunction();
  23. }
  24.  
Runtime error #stdin #stdout #stderr 0s 4172KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
terminate called after throwing an instance of 'std::bad_weak_ptr'
  what():  bad_weak_ptr