#include <OTC/collctn/collctn.hh> template<class T> class OTC_Collection {
public:
virtual ~OTC_Collection();
OTC_Collection();
virtual void removeAll();
virtual OTC_Boolean isEmpty() const;
virtual u_int population() const;
virtual OTC_Iterator<T> items( OTC_Protection theProtection=OTCLIB_SAFE ) const;
};
void sum(OTC_Collection<int>& theColl)
{
int theSum = 0;
if (!theColl.isEmpty())
{
OTC_Iterator<int> aIter = 0;
anIter = theColl.items();
for (aIter.reset(); aIter.isValid(); aIter.next())
theSum += aIter.item();
theColl.removeAll();
}
cout << theSum << endl;
}
If a function only requires the ability to iterate over a
collection as above, the function should be written to accept the
collection base class. This would allow any type of collection to
be passed to the function and not just the particular one that
was being used at that time.
OTC_Collection();
virtual void removeAll();
virtual OTC_Boolean isEmpty() const;
OTCLIB_TRUE
if the collection is
empty.
virtual u_int population() const;
virtual OTC_Iterator<T> items(
OTC_Protection theProtection=OTCLIB_SAFE
) const;
removeAll()
, population()
and items()
must be redefined in a
derived class. isEmpty()
should also be redefined in a derived
class if possible, as the default for isEmpty()
is to compare
the result of population()
against 0
. This is potentially
inefficient, depending on the implementation of a particular
collection.
It is possible to create an instance of the OTC_Collection
class. If you do, it will behave like an empty collection,
always returning a population of 0
and a null iterator.
OTC_Iterator