Try this one:
template<typename T>
class A
{
public:
class Iterator : public std::iterator<std::forward_iterator_tag, T>
{
public:
virtual void DoSomething() = 0;
};
virtual Iterator * begin() const = 0;
virtual void func() = 0;
};
template<typename T>
class B : public A<T>
{
public:
B() {}
class BIterator : public A<T>::Iterator
{
public:
void DoSomething()
{
std::cout << "Hello world from B::BIterator!";
}
};
A<T>::Iterator * begin() const
{
return new BIterator();
}
virtual void func()
{
}
};
int main(int argc, char * argv[])
{
B<int> b;
A<int>::Iterator * iter = b.begin();
iter->DoSomething();
delete iter;
getchar();
}
Note, that this won't force B to implement a new instance of A<T>::Iterator
, but simply to provide one. Developer may, for instance, implement a single generic iterator and return it from all A<T>
-derived classes. But I guess, that should be enough, shouldn't it?
Edit: In response to comments
You should return the iterator by pointer. Since you want it to be an abstract class, you cannot declare instance of it like:
A<int>::Iterator iter = b.begin(); // Error: cannot instantiate abstract class
// A<int>::Iterator
You can do it however if you resign from A<T>::Iterator
being an abstract class. But then you won't be able to force one to descend from A<T>::Iterator
...