I need to use a shared_ptr here because I can’t change the API.
Foo1 *foo1 = new Foo1(...);
shared_ptr<Foo2> foo2(foo1);
Is the shared_ptr here going to handle freeing the memory used by foo1? If I understand correctly, I shouldn’t have to call delete on foo1 correct?
Yes. You are correct, but the correct way to initialise
foo2is:Herb Sutter discusses the reasons why you should use
std::make_shared<>()here:https://herbsutter.com/2013/05/29/gotw-89-solution-smart-pointers/