Photo by Roman Synkevych

Smart-Pointer - Constructors

This article examines constructors that are often overlooked. It examines their use cases and explains why the added functionality is meaningful in relation to smart pointers. Default Constructor Most people remember the default constructor (a zero-argument constructor), but it is sometimes overlooked. The default constructor is useful when the type is used in a context where objects of the type need to be instantiated dynamically by another library (an example is a container resized; when a container is made larger by a resize, new members will need to be constructed, it is the default constructor that will provide these extra instances). ...

January 23, 2015 · 7 min · Loki Astari, (C)2015
Photo by Ken Blode

Smart-Pointer - Shared Pointer

So in the previous article, I covered a basic unique pointer where the smart pointer retained sole ownership of the pointer. The shared pointer (SP) is the other common smart pointer we encounter. In this case, the ownership of the pointer is shared across multiple instances of SP, and the pointer is only released (deleted) when all SP instances have been destroyed. So, not only do we have to store the pointer, but we also need a mechanism to keep track of all the SP instances that share ownership of the pointer. When the last SP instance is destroyed, it also deletes the pointer (The last owner cleans up., A similar principle to the last one to leave the room turns out the lights). ...

January 15, 2015 · 7 min · Loki Astari, (C)2015
Photo by ThisisEngineering RAEng

Smart-Pointer - Unique Pointer

On codereview.stackexchange.com in the C++ tag, it seems like a write of passage to implement your own version of a smart pointer. A quick search brings up the following: 02/Sep/2011 - shared_ptr implementation 26/Nov/2011 - Shared Pointer implementation 18/Apr/2013 - Request for review: reference counting smart pointer 20/May/2013 - Efficient smart pointer implementation in C++ 11/Aug/2013 - C++98 Unique Pointer Implementation 14/Aug/2013 - I wrote a class to implement auto_ptr 28/Aug/2013 - yet another shared pointer 04/Mar/2014 - Smart pointer implementation 13/May/2014 - One more shared pointer 14/Jun/2014 - Is this a meaningful Intrusive Pointer Class? 04/Aug/2014 - Simple shared pointer 08/Oct/2014 - Smart but simple pointers 15/Nov/2014 - Simple auto_ptr 19/Dec/2014 - Yet another smart pointer implementation for learning Writing your own implementation of a smart pointer is a bad idea (IMO). The standardization and testing of smart pointers was a nine year process through boost, with boost::shared_ptr and boost::scoped_ptr, finally resulting in the standardized versions being released in C++11: std::shared_ptr and std::unique_ptr. ...

December 30, 2014 · 8 min · Loki Astari, (C)2014