Item 9:Use destructors to prevent resource leaks. (More Effective C++)
The solution is to replace the pointer pa
with an object that acts like a pointer. That way, when the pointer-like object is (automatically) destroyed, we can have its destructor call delete
. Objects that act like pointers, but do more, are called smart pointers, and, as Item 28 explains, you can make pointer-like objects very smart indeed. In this case, we don't need a particularly brainy pointer, we just need a pointer-like object that knows enough to delete what it points to when the pointer-like object goes out of