How to write a base class and several derived classes of iterator?
Does the iterator have to return itself (*this)?
So far, I use typename X
and static_cast<X&>(*this)
to allow the derived class to inherit a function that return itself from the base class.
This
looks ugly. Is there a better way?
Simplified Code:
#include <iterator>
#include <iostream>
template <typename T, typename X>
class BaseIterator : public std::iterator<std::input_iterator_tag, T> {
//Not intended to be used directly.
private:
T* p;
protected:
virtual void increment(void)=0;
virtual T* stride_index(int index)=0;
public:
virtual ~BaseIterator(){} //virtual destructor.
X operator++(int) { //takes a dummy int argument
X tmp(static_cast<X&>(*this) );
increment();
return tmp;
}
bool operator==(const X & rhs) { return p==rhs.p; }
} ;
template <typename T>
class ContiguousIterator : public BaseIterator<T, ContiguousIterator<T> > {
private:
T* p;
protected:
inline void increment(void) {++p;}
inline T* stride_index(int index){return p + index;}
public:
virtual ~ContiguousIterator(){} //destructor.
ContiguousIterator(T* x) :p(x) {}
ContiguousIterator(const ContiguousIterator<T> & mit) : p(mit.p) {}
} ;
int main(void){
int i[]={0,1,2,3,4,5};
ContiguousIterator<int> itbegin(i);
ContiguousIterator<int> it(i);
it++;
std::cout << "result: " << (it == itbegin) << std::endl;
}
The alternative is to forget about using inheritance to write less code. Just copy and paste the function that return *this
to the derived classes.
That alternative seems increasingly acceptable to me ...