### Using Managed Pointers

Managed pointers keep a reference count for each referenced object (element). When the last managed pointer to the object is destructed, the object is automatically deleted. You should use managed pointers when you are unsure who is responsible for deleting an object. This may occur where several pointers to an object are introduced over time, and the order in which the pointers are released is not known.

The following example shows how to use pointers from the IMngElemPointer class:

    //main.cpp - main file
    #include "person.h" //person.h from the previous examples
    #include <istdops.h>
    #include <iset.h>

    typedef IMngElemPointer <PersonPtr> MEPersonPtr;
    typedef ISet <MEPersonPtr> AddressList;

    ostream& operator<<(ostream& os,Person A)
    {
        return (os << endl << A.GetPersonName() << " " <<
            A.GetTNumber());
    }

    void main()
    {
        AddressList Business;
        AddressList::Cursor myCursor1(Business);

        MEPersonPtr Aptr (new Person("Peter Black","714-50706"),IINIT);
        MEPersonPtr Bptr (new Person("Carl Render","714-540321"),IINIT);
        MEPersonPtr Cptr (new Person("Sandra Summers","x"),IINIT);
        MEPersonPtr Dptr (new Person("Mike Summers","x"),IINIT);
        MEPersonPtr CopyCptr (new Person("Sandra Summers","x"),IINIT);

        Business.add(Aptr);
        Business.add(Bptr);
        Business.add(Cptr);
        Business.add(Dptr);
        Business.add(CopyCptr);

        forICursor (myCursor1) {
            cout << *Business.elementAt(myCursor1);
        }

        Business.remove(Cptr); //Remove pointer from collection
    }

After removing the pointer from the collection, the managed pointer is automatically deleted. In the example, the allocated Person will automatically be deleted by the remove function unless it is referenced through another PersonPtr.

Introduction to the Collection Classes
Collection Class Hierarchy
Overall Implementation Structure
Adding Elements
Removing Elements
Replacing Elements
Smart Pointers

Choosing the Appropriate Smart Pointer Class
Constructing Smart Pointers
Using Automatic Pointers
Using Element Pointers
Things to Watch Out For When Using Smart Pointers
Copying and Referencing Collections
Instantiating the Collection Classes