How do I provide virtual 'iterators' ?
I have a 'Graph' class, which has derived classes for Adjacency Matrix and Adjacency List representations.
How do I provide iterators for traversing vertices and edges, when the iterator classes would have different implementations for the different derived classes ?
The following way is the only one I can think of, but seems quite cumbersome.
Code:
class Base
{
public:
class BaseIterator
{
};
virtual const BaseIterator& begin();
virtual const BaseIterator& end();
};
class Foo:public Base
{
public:
class FooIterator:public Base::BaseIterator
{
};
//Implementation of the functions
};
class Bar:public Base
{
public:
class BarIterator:public Base::BaseIterator
{
};
//...
};
Or is there a pattern for doing this that I'm not aware of ?
Would composition be a better idea here compared to polymorphism ?
I mean, I can think like..a Graph can 'have' several representation 'objects' within it.
Edit: All the involved classes are templates,not sure if that makes the situation different.