Skip to the content.

Object Ownership & Smart Pointers

Back to Homepage
Back to Week 3


Relationship Type Review

Is-a: Inheritance

Holds-a

Has-a


Smart Pointers

#include <memory>

std::unique_ptr // 
std::shared_ptr // Can have multiple shared pointers that point to the same object
std::weak_ptr

shared_ptr blah(new Person()); // Shared pointer contains person pointer, also creates a reference count to keep track of how many pointers are pointing at that thing.

// When reference count becomes zero it will delete Person

Unique pointer

Share pointers have share ownership

Weak pointer