Smart Pointers (8b) Flashcards
1
Q
What is a smart pointer?
A
Class type that look and act like a pointer
Must overload -> and *
Usually a template class
2
Q
Why are smart pointers useful?
A
Define ctor and dtor of pointer (set = nullptr)
Can make them automatically delete object pointing to
Can change dereferencing
3
Q
What is a unique_ptr?
A
Smart pointer that cleans up after itself
Only one unique_ptr can manage an object
4
Q
What are the uses of a unique_ptr?
A
Guarantees deletion Moving ownership of uniquely-owned objects with dynamic lifetimes into and from methods Use release(), reset(), swap() to change ownership
5
Q
What is a shared_ptr?
A
Smart pointer that provides reference counting
Tracks how many objs point to a particular resource and auto deletes resource when no pointers point to it