Smart Pointers Flashcards
Smart Pointers
What does RAII stand for?
Resource Acquisition is Initialization
What are the other two Acronyms RAII can be referred to as?
- CADRe (CTORs Acquire DTORs Release)
- SBRM (Scope Based Resource Management)
What happens when a class falls out of scope?
Its DTOR is called
What would prevent a stack variable’s DTOR from being called?
An unchecked error (uncaught throw or segfault)
What is the first concept behind RAII?
That resource acquisition should be done during intialization.
What is the importance Acquiring during Initialization?
Moving past this point confirms that all data was initizialized correctly.
What is the second concept behind RAII?
That resources must be released when data is deallocated. (delete pointers, close files, etc.)
What are Smart Pointers?
Objects that act as pointers.
What library contains smart pointers?
<memory>
What are the benefits of using smart pointers?
- Allow for CTORS and DTORS
- They can be put on the stack and passed around more easily
- Methods for working with data
What are the 3 types of Smart Pointers?
- Unique
- Shared
- Weak
What is a Unique Pointer?
A smart pointer that exclusively owns one object. It handles allocation, and deallocation of the object.
How do you syntactically declare a unique pointer?
unique_ptr<type> ptrName(new type);
What must the objects held by unique pointers have?
A DTOR. The unique_ptr will try to call this when deallocation occurs.
When must you have a virtual DTOR?
When you have a Data Structure containing smart pointers to an inheritable object. Ensures DTORs can be called polymorphically.