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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is a unique_ptr?

A

Smart pointer that cleans up after itself

Only one unique_ptr can manage an object

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly