Choosing the Appropriate Smart Pointer Class

The following features of Collection Classes pointer types give you the choices shown in the table below. Standard C++ pointers are included for comparison.

  Destruction of Pointed Objects
Not managed When out-of-scope Reference counted
Collections call element operations on pointer Standard C++ pointer IAutoPointer IMngPointer
Collections call element operations on referenced object IElemPointer IAutoElemPointer IMngElemPointer

Smart pointers can only take arguments of type class or struct. This is because the overloaded operator-> needs to return an object of such a type. You can apply pointer objects from these five classes in the same way you use ordinary C++ pointers, with the * and -> operators. Elements are implicitly deleted except in the case of IElemPointer. To delete an element referred to by an IElemPointer you must use an explicit conversion to the referenced element type:

IElemPointer<E> ptr;
// ...
delete (E*) ptr;

Element Functions and Elements Referenced by Pointers

If you want element functions to work on the elements referenced by the pointers, the Collection Classes offer the IElemPointer, IAutoElemPointer and IMngElemPointer pointer classes, which are instantiated with the element type. Pointers of these classes automatically apply all element functions, except for assignment, to the referenced object. Element pointers are constructed from C++ pointers. The C++ dereferencing operators * and -> are defined, for element pointers, to refer to the referenced objects.

The dynamically created elements are not automatically deleted when they are removed from the collection.



Introduction to the Collection Classes
Collection Class Hierarchy
Overall Implementation Structure
Smart Pointers


Constructing Smart Pointers
Using Automatic Pointers
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
Taking Advantage of the Abstract Class Hierarchy