What is enable_shared_from_this in cpp

Problem class C : public enable_shared_from_this<C> { public: std::shared_ptr<C> func() { std::shared_ptr<C> local_sp_a(this); return local_sp_a; } }; int main() { C c; auto a = c.func(); return 0; } // Output: // ./a.out // double free or corruption (out) // fish: Job 1, './a.out' terminated by signal SIGABRT (Abort) Why it is because when a shared_ptr was created, the control block was build up too, and there are three methods can create control block ...

July 3, 2023 · 1 min · hengist