Intro

In C++, smart pointers are objects that own and manage the lifetime of a dynamically allocated object.

  • They automatically delete the object when it’s no longer used, preventing memory leaks.

  • Common types:

    • std::shared_ptr<T> – multiple owners; object is deleted when the last owner is gone.
    • std::unique_ptr<T> – single owner; object is deleted when that owner goes out of scope.
    • std::weak_ptr<T> – non-owning reference to a shared_ptr (prevents cycles).