Smart Pointer - C++ Smart Pointers

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,

auto_ptr obvious_function1;

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:

    Some smart man once said that on the most exalted throne in the world we are seated on nothing but our own arse.
    Wendell Mayes, U.S. screenwriter. Otto Preminger. CINCPAC II (Henry Fonda)