Skip to main content
Journey Uncommon Logo
JourneyUncommon
mediumC++shared_ptr weak_ptrSingle-choice MCQ

A class wants to hand out a shared_ptr to itself from a member function. Why must it inherit from std::enable_shared_from_this rather than just writing return std::shared_ptr<T>(this);?

struct Node { std::shared_ptr<Node> self() { return shared_from_this(); } };