Values & References16 / 16
Up next

shared_ptr shares ownership

#include <print>
#include <memory>
#include <vector>

int main() {
    auto a = std::make_shared<std::vector<int>>(std::vector<int>{1, 2, 3});
    // a second owner bumps the reference count
    auto b = a;
    std::println("owners: {}", a.use_count());
    std::println("{}", (*b)[0]);
}
What you're solving

freed when the last owner drops