C++ Smart Pointers
In C++, smart pointers may be implemented as a template class that mimics, by means of operator overloading, the behaviour of traditional (raw) pointers, (e.g. dereferencing, assignment) while providing additional memory management algorithms.
Smart pointers can facilitate intentional programming by expressing the use of a pointer in the type itself. For example, if a C++ function returns a pointer, there is no way to know whether the caller should delete the memory pointed to when the caller is finished with the information.
some_type* ambiguous_function; // What should be done with the result?Traditionally, this has been solved with comments, but this can be error-prone. By returning an auto_ptr
,
the function makes explicit that the caller will take ownership of the result, and furthermore, that if the caller does nothing, no memory will be leaked.
Read more about this topic: Smart Pointer
Famous quotes containing the word smart:
“Among the Very Rich you will never find a really generous man, even by accident. They may give their money away, but they will never give themselves away; they are egoistic, secretive, dry as old bones. To be smart enough to get all that money you must be dull enough to want it.”
—Gilbert Keith Chesterton (18741936)