Smart Pointers

In C++, variables and function arguments have their values copied when they are assigned. This copying can decrease a program's efficiency, especially when the objects are large. To improve efficiency, pointers or references are often used for common objects. For example, a pointer or reference to the object can be copied, instead of the object itself.

For efficiency, pointers to elements can be used as collection element types, rather than the elements themselves. References are not allowed as collection element types.

The Collection Classes define five pointer classes, called smart pointers. You can store these smart pointers, as well as standard C++ pointers, as elements in any collection:

Smart Element Pointers

IElemPointer, IAutoElemPointer and IMngElemPointer are special smart pointers that are designed to be kept as elements in a collection. If you store standard C++ pointers in a collection, the collection performs all element and key-type functions, except assignment, on the pointers themselves rather than on the object to which the pointer refers. This is not always what you intend. If you want the collections to perform element functions, such as equality test, on the referenced elements instead, use one of the smart element pointers (one of the classes named IxxxElemPointer). They are objects which behave as pointers to the actual element. With the element pointers, the elements themselves are not stored in the collection, although information from the elements is used by Collection Classes functions.

Automatic Storage Management

IAutoPointer, IAutoElemPointer, IMngPointer and IMngElemPointer perform storage management, which means that under certain conditions they automatically delete the object to which they refer. Automatic pointers (IAutoPointer and IAutoElemPointer) automatically delete the referenced object when the automatic pointer instance is destructed or the automatic pointer is used to point to another object. Managed pointers (IMngPointer and IMngElemPointer) keep a reference count for each referenced object. The referenced object of a managed pointer is deleted only when the last managed pointer to the object is destructed.

IElemPointer does not provide automatic storage deallocation.

Generally Useful

Automatic storage management is particularly useful when functions return pointers or references to objects that they have created (dynamically allocated), and the last user of the object is responsible for cleaning up.

To exploit the advantage of memory management, you can use non-element smart pointers (IAutoPointer and IMngPointer), instead of standard C++ pointers, without storing them in a collection. They behave similarly to standard C++ pointers. For example, if you check the equality of two such pointers from your collection of pointers, true is only returned if the pointers point to the same address (this is the same behavior as you would expect for native C++ pointers).



Introduction to the Collection Classes
Element Functions and Key-Type Functions
Access by Key
Equality Relation
Adding Elements
Removing Elements
Replacing Elements


Choosing the Appropriate Smart Pointer Class
Constructing Smart Pointers
Using Automatic Pointers
Assigning One Automatic Pointer to Another
Holding Automatic Pointers in a Collection
Transferring Automatic Pointers Between Functions
Using Element Pointers
Using Managed Pointers
Things to Watch Out For When Using Smart Pointers
Adding an Element to a Collection
Removing an Element from a Collection
Instantiating the Collection Classes