Tuesday, September 16, 2014

Unique Smart Pointers

Another C++11 STL smart pointer class are unique pointers (std::unique_ptr).  This smart pointer is for a single scope and deletes its resource when it goes out of scope (like when a function returns).  If used for a class member variable, it deletes its resource when the class instance is deleted or goes out of scope.

Most of the rest of the allocated resources were changed to these unique pointers.  Two of these were in local blocks of functions.  The rest were members of various classes.  For all the classes involved, after the delete operators were removed from the destructors, there was no code left, so the destructors were removed.  A default constructor is now generated, which calls the destructors for the unique pointers causing their resources to be deleted.

One drawback to using unique pointers is that forward references can no longer be used in the header files for the classes involved.  Forward references could previously be used since only a pointer to the class was used.  However, with unique pointers, the size of the class is needed.  Therefore, the forward references were replaced with include statements for the headers of the classes.

These changes covered all the remaining naked new and delete operations except for one.  This remaining naked resource is the string (QString) pointers kept in a vector (QVector) for the constant string information used in the constant string dictionary (that holds the constant strings in the BASIC program).  There is probably a better way to implement this and so will be handled separately.

[branch cpp11 commit 95c680e107]