Join Bill Weinman for an in-depth discussion in this video Using a custom deleter, part of C++: Smart Pointers.
- Sometimes when destroying a smart pointer,…you may need it to do more…than just destroy the managed object.…For these circumstances,…you may define a custom deleter.…Here I have a working copy of custom_deleter.cpp…from the Exercise Files…and I also have the strc class opened in this project.…You'll notice here when I create the smart pointer,…that I'm specifying here the address,…a pointer to this deleter function…and here's the deleter function.…
And the deleter function prints out…the value from the object.…It gets past the bare pointer to the object.…It's not past the smart pointers.…It's past the bear pointer or the object.…It displays its value and then it uses the delete operator…to free the resource.…So the custom deleter is responsible…for freeing the resource…and of course it can do whatever else it needs to do…as part of the reason that you needed a custom deleter.…Obviously just to call delete.…
You're not gonna need one,…but this is just an example.…So you specify the deleter…in the smart pointer's constructor…
Share this video
Embed this video
Video: Using a custom deleter