One of the biggest issues that arise in C++ is who owns a particular pointer.
For example consider the simple function declaration below.
The caller of this function does not know at first glance whether then need to call delete on the pointer or whether they are just borrowing the pointer and library will take care of releasing the resource. If the caller deletes the resource when they are not supposed to bad things will happen most likely
and we will run into the land of undefined behaviour. If we were supposed to call delete then we have leaked the resource. The only thing we can really hope for is either the documentation is good or we can read the source to determine the right course of action.
If this leaves you feeling pretty unsatisfied read on. We can help improve on this by making this
explicit in our own code. Enter observable_ptr. This is a user defined type that models the
notion of a pointer that is borrowed and someone else is responsible for the cleanup. We are mearly
just observers.
Before we jump in for those who have not overloaded the * (dereference) and -> (arrow) operators this will be explained in depth as we go.
C++, pointers, non owning
I feedback.
Let me know what you think of this article in the comment section below!