So what are smart pointers? Well, smart pointers are objects.
In fact, smart pointers are implemented as c++
template classes, which we can instantiate.
Smart pointers can only point to heap allocated memory,
and they automatically call delete when that's no longer needed.
Also very importantly, smart pointers adhere to RAII principles.
As previously mentioned, c++ has several types of smart pointers:
unique pointer, shared pointer, weak pointer and auto pointer.
Auto pointer has been deprecated so you shouldn't use this going forward,
and we won't discuss it in this section. So now let's see how we can create a smart pointer.
First, we need to include the memory header file since it defines the smart pointer
classes and functions we'll use.
As previously mentioned, smart pointers are implemented as class templates,
very similar to the way that the vector class is implemented,
and we create them using a template parameter, much as we did with vectors.
Smart pointers are basically wrapper classes that contain and manage
a raw pointer.
The actual implementation of smart pointers is compiler dependent
and is beyond the scope of this section.
Once we create a smart pointer, we can use the smart pointer
much in the same way as we use the raw pointer.
The smart pointer classes provide overloaded dereference and member selection operators
as well as methods to get the raw pointer and more.
However, you can't do pointer arithmetic on smart pointers, like you can with raw pointers.
Finally, we can have custom deleters with smart pointers
where we can explicitly define the exact behavior we want when the pointer is about to be destroyed.
Now let's see the syntax for declaring a smart pointer.
Here's an example of how you might create a smart pointer variable.
Of course, you would replace smart pointer
with one of the c++ smart pointer types,like unique pointer,
shared pointer or weak pointer.
In this case, the smart pointer ptr
will manage an object of type sum class that's been allocated on the heap.
Once we create a smart pointer object,
we can use the dereference operator, the member selection operator
or any of the other smart pointer member methods.
Notice that we don't have to explicitly delete the pointer when we're done with it.
In this example, when the pointer goes out of scope,
the smart pointer's destructor is called automatically
and it takes care of de-allocating any heap storage that was used.
That's pretty cool. You can see how powerful smart pointers are.
They can prevent memory leaks, make your code easier to read and write and maintain
with less errors.
Now let's talk about a common idiom or design pattern used in c++ programming.
It's called RAII
and it stands for Resource Acquisition Is Initialization.
A complicated acronym for a pretty simple concept.
First, RAII objects are allocated on the stack.
The resource acquisition part is pretty straightforward.
It means that RAII objects will acquire some sort of resource
that could be opening a file, allocating memory, acquiring a lock and so forth.
The initialization part is also straightforward.
This means that the resource is acquired at object initialization time.
So in other words, it happens in the object's constructor.
Now at some point we need to relinquish that resource that we acquired,
and that happens in the destructor.
Here we can close the file, de-allocate any allocated memory,
release locks and so forth.
Smart pointers are examples of RAII classes
since this is the idiom they follow with regard to memory resources,
a simple concept but a very powerful concept.
So let's start our exploration of the c++ smart pointers with the unique pointer
in the next video.