Smart Pointers as Pointers
Explore how to apply const with smart pointers in C++. Understand the difference between const pointers and const pointed values, learn the implications for pointer resets and modifications, and discover best practices for using const with std::unique_ptr to improve code safety.
We'll cover the following...
There are two ways to approach smart pointers. One way is to consider that smart pointers are effective pointers. Either the pointer or the pointed value can be const. Or both.
In another perspective, we consider that smart pointers are class-type objects. Basically, they are wrapping pointers. As a smart pointer is an object, the rule of thumb says that it can be passed around as a const reference. We are going to see that in most cases, it’s a bad idea.
Let’s examine both perspectives.
const and smart pointers as pointers
Smart pointers are ...