Copying and Referencing Collections

The Collection Classes implement no structure sharing between different collection objects. The assignment operator and the copy constructor for collections are defined to copy all elements of the given collection into the assigned or constructed collection. You should remember this point if you are using collection types as arguments to functions. If the argument type is not a reference or pointer type, the collection is passed by the copy constructor, and changes made to the collection within the called function do not affect the collection in the calling function.

If you want a function to modify a collection, pass the collection as a reference:

void removeListMember (AddressList aList) { /* ... */ }   // wrong
void removeListMember (AddressList & aList) { /* ... */ } // right

For the sake of efficiency, avoid having a collection type as the return type of a function:

AddressList f()
{
    AddressList aList;
    // ...
    return aList;
}

Business=f();  //Very inefficient

In this program Business becomes a reference argument to the assignment operation, which would again copy the set. A better approach is:

void f(AddressList& aList) { /* ... */ }
// ...
f(Business);


Introduction to the Collection Classes
Collection Class Hierarchy


Memory Management with Element Operation Classes
Guard Objects
Instantiating a Guard Object
Using Guard Objects
Taking Advantage of the Abstract Class Hierarchy
Instantiating the Collection Classes