Enum die 2.

Ich hab mal ne noch krausere Implementierung des ContainerEnumerator des vorherigen posts geschrieben.

Diesmal wird sie so benutzt:


vector<int> vInts;
vInts.push_back(1);
enumiterator<vector <int> > enumIt(&vInts);

while(enumIt++)
{
...
}

C++:
  1. template <class _T, class _I = _T::iterator>
  2. class enumiterator : public _I
  3. {
  4. public:
  5.     enumiterator(_T* pContainer)
  6.     {
  7.         *((_I*)this) = pContainer->begin();
  8.         m_pContainer = pContainer;
  9.     }
  10.     bool operator++()
  11.     {
  12.         if(*this != m_pContainer->end())
  13.         {
  14.             (*((_I*)this))++;
  15.             return true;
  16.         }
  17.         else
  18.             return false;
  19.     }
  20.     bool operator++(int)
  21.     {
  22.         if(*this != m_pContainer->end())
  23.         {
  24.             (*((_I*)this))++;
  25.             return true;
  26.         }
  27.         else
  28.             return false;
  29.     }
  30. protected:
  31.     _T*  m_pContainer;
  32. };

Leave a Reply