When should we use std::enable_shared_from_this
问题 I just knew std::enable_shared_from_this form this link. But after reading the code below, I don't know when to use it. try { Good not_so_good; std::shared_ptr<Good> gp1 = not_so_good.getptr(); } catch(std::bad_weak_ptr& e) { // undefined behavior (until C++17) and std::bad_weak_ptr thrown (since C++17) std::cout << e.what() << '\n'; } The code above is "not so good" because there is no existing shared_ptr before calling getptr() . So the good thing should be: std::shared_ptr<Good> gp1 = std: