fork download
  1. #include <memory>
  2.  
  3. struct Base : std::enable_shared_from_this<Base>
  4. {};
  5.  
  6.  
  7. struct Derived : Base
  8. {
  9. std::shared_ptr<Derived> shared_from_this() { return std::static_pointer_cast<Derived>(Base::shared_from_this()); }
  10. };
  11.  
  12. main()
  13. {
  14. std::shared_ptr<Derived> ptr(new Derived);
  15.  
  16. std::shared_ptr<Derived> d_ptr = ptr->shared_from_this();
  17.  
  18. std::shared_ptr<Base> b_ptr = ptr->shared_from_this();
  19. }
Success #stdin #stdout 0s 3060KB
stdin
Standard input is empty
stdout
Standard output is empty