std::shared_ptr allows several owners to share one object safely.

Shared Ptr

shared_ptr.cpp
#include <iostream>
#include <memory>

class Counter {
private:
    int value;

public:
    Counter(int start) {
        value = start;
    }

    void add(int amount) {
        value += amount;
    }

    int current() {
        return value;
    }
};

int main() {
    int start = ;
    int amount = ;

    std::shared_ptr<Counter> first = std::make_shared<Counter>(start);

    {
        std::shared_ptr<Counter> second = first;
        second->add(amount);
        std::cout << "insideOwners=" << first.use_count() << std::endl;
    }

    std::cout << "outsideOwners=" << first.use_count() << std::endl;
    std::cout << "value=" << first->current() << std::endl;
    return 0;
}
#include <iostream>
#include <memory>

class Counter {
private:
    int value;

public:
    Counter(int start) {
        value = start;
    }

    void add(int amount) {
        value += amount;
    }

    int current() {
        return value;
    }
};

int main() {
    int start = ;
    int amount = ;

    std::shared_ptr<Counter> first = std::make_shared<Counter>(start);

    {
        std::shared_ptr<Counter> second = first;
        second->add(amount);
        std::cout << "insideOwners=" << first.use_count() << std::endl;
    }

    std::cout << "outsideOwners=" << first.use_count() << std::endl;
    std::cout << "value=" << first->current() << std::endl;
    return 0;
}
#include <iostream>
#include <memory>

class Counter {
private:
    int value;

public:
    Counter(int start) {
        value = start;
    }

    void add(int amount) {
        value += amount;
    }

    int current() {
        return value;
    }
};

int main() {
    int start = ;
    int amount = ;

    std::shared_ptr<Counter> first = std::make_shared<Counter>(start);

    {
        std::shared_ptr<Counter> second = first;
        second->add(amount);
        std::cout << "insideOwners=" << first.use_count() << std::endl;
    }

    std::cout << "outsideOwners=" << first.use_count() << std::endl;
    std::cout << "value=" << first->current() << std::endl;
    return 0;
}
#include <iostream>
#include <memory>

class Counter {
private:
    int value;

public:
    Counter(int start) {
        value = start;
    }

    void add(int amount) {
        value += amount;
    }

    int current() {
        return value;
    }
};

int main() {
    int start = ;
    int amount = ;

    std::shared_ptr<Counter> first = std::make_shared<Counter>(start);

    {
        std::shared_ptr<Counter> second = first;
        second->add(amount);
        std::cout << "insideOwners=" << first.use_count() << std::endl;
    }

    std::cout << "outsideOwners=" << first.use_count() << std::endl;
    std::cout << "value=" << first->current() << std::endl;
    return 0;
}
#include <iostream>
#include <memory>

class Counter {
private:
    int value;

public:
    Counter(int start) {
        value = start;
    }

    void add(int amount) {
        value += amount;
    }

    int current() {
        return value;
    }
};

int main() {
    int start = ;
    int amount = ;

    std::shared_ptr<Counter> first = std::make_shared<Counter>(start);

    {
        std::shared_ptr<Counter> second = first;
        second->add(amount);
        std::cout << "insideOwners=" << first.use_count() << std::endl;
    }

    std::cout << "outsideOwners=" << first.use_count() << std::endl;
    std::cout << "value=" << first->current() << std::endl;
    return 0;
}
#include <iostream>
#include <memory>

class Counter {
private:
    int value;

public:
    Counter(int start) {
        value = start;
    }

    void add(int amount) {
        value += amount;
    }

    int current() {
        return value;
    }
};

int main() {
    int start = ;
    int amount = ;

    std::shared_ptr<Counter> first = std::make_shared<Counter>(start);

    {
        std::shared_ptr<Counter> second = first;
        second->add(amount);
        std::cout << "insideOwners=" << first.use_count() << std::endl;
    }

    std::cout << "outsideOwners=" << first.use_count() << std::endl;
    std::cout << "value=" << first->current() << std::endl;
    return 0;
}
#include <iostream>
#include <memory>

class Counter {
private:
    int value;

public:
    Counter(int start) {
        value = start;
    }

    void add(int amount) {
        value += amount;
    }

    int current() {
        return value;
    }
};

int main() {
    int start = ;
    int amount = ;

    std::shared_ptr<Counter> first = std::make_shared<Counter>(start);

    {
        std::shared_ptr<Counter> second = first;
        second->add(amount);
        std::cout << "insideOwners=" << first.use_count() << std::endl;
    }

    std::cout << "outsideOwners=" << first.use_count() << std::endl;
    std::cout << "value=" << first->current() << std::endl;
    return 0;
}
#include <iostream>
#include <memory>

class Counter {
private:
    int value;

public:
    Counter(int start) {
        value = start;
    }

    void add(int amount) {
        value += amount;
    }

    int current() {
        return value;
    }
};

int main() {
    int start = ;
    int amount = ;

    std::shared_ptr<Counter> first = std::make_shared<Counter>(start);

    {
        std::shared_ptr<Counter> second = first;
        second->add(amount);
        std::cout << "insideOwners=" << first.use_count() << std::endl;
    }

    std::cout << "outsideOwners=" << first.use_count() << std::endl;
    std::cout << "value=" << first->current() << std::endl;
    return 0;
}
#include <iostream>
#include <memory>

class Counter {
private:
    int value;

public:
    Counter(int start) {
        value = start;
    }

    void add(int amount) {
        value += amount;
    }

    int current() {
        return value;
    }
};

int main() {
    int start = ;
    int amount = ;

    std::shared_ptr<Counter> first = std::make_shared<Counter>(start);

    {
        std::shared_ptr<Counter> second = first;
        second->add(amount);
        std::cout << "insideOwners=" << first.use_count() << std::endl;
    }

    std::cout << "outsideOwners=" << first.use_count() << std::endl;
    std::cout << "value=" << first->current() << std::endl;
    return 0;
}
shared ownership A `std::shared_ptr` keeps the object alive until the last shared owner is gone.