Transferring Ownership
Back to Homepage
Back to Week 3
Smart Pointers
- unique
- only one
- shared
- delete when last one is destroyed
- weak
- option to get a shared pointer
- Shared pointer that doesnt contribute to the count
- If the thing got deleted, pointer will say it doesnt exist anymore
- allows for easier memory management than raw pointers
- allows more control than garbage collection
class List {
unique_ptr<int[]>backingArray;
}
Copy
Move
RegistrationForm -> unique_ptr(Person) createPerson() -> return unique_ptr(new Person())
// Because person will outlive form, maybe return pointer as person will be on heap
// Dont want to tie their lifetimes together
make_unique<Person>(...); // Creates a new person without using new keyword