This recipe will show the basic usage of unique_ptr and shared_ptr. These smart pointers are the main helpers for programmers who don't want to deal with memory deallocation manually. Once you've learned how to use them properly, this will save headaches and nights of debugging sessions.
Smart pointers – unique_ptr and shared_ptr
How to do it...
In this section, we'll look at the basic use of two smart pointers, std::unique_ptr and std::shared_ptr:
- Let's develop a unique_ptr example by developing the following class:
#include <iostream>
#include <memory>
class CruiseControl
{
public:
CruiseControl()
{
std::cout << "CruiseControl object created" <...