R
richard.forrest1
I have a problem with an abstract interface class whose implementation
classes need to return different iterator types (but with the same
value_types etc).
Classes A and B both conform to the same abstract Interface class. Interface
has a pair of virtual functions begin() and end() that generate a typical
STL style range. Classes A and B provide different implementations, maybe
using different types of container. The problem is that, to conform to the
interface, A and B must return the same type (or a derived type). They can
not do this directly as their containers (and hence their iterators) are of
different types.
class Interface
{
public:
virtual VirtualIterator begin() = 0;
virtual VirtualIterator end() = 0;
};
class A : public Interface
{
public:
virtual VirtualIterator begin() {return
make_virtual_iterator(container_.begin());}
virtual VirtualIterator end() {return
make_virtual_iterator(container_.end());}
private:
OneKindOfContainer container_;
};
class B : public Interface
{
public:
virtual VirtualIterator begin() {return
make_virtual_iterator(container_.begin());}
virtual VirtualIterator end() {return
make_virtual_iterator(container_.end())}
private:
AnotherKindOfContainer container_;
};
I think I see how to solve this problem by creating (as the example hints
at) a kind of VirtualIterator class that maintains a pointer to heap based
copy of the real iterator. But this issue must be fairly common and I would
much rather reuse a tried and tested solution than roll my own. I looked at
the boost iterator library but it does not seem to provide what I am looking
for. This made me a bit concerned because it seems to be very well thought
out and comprehensive. Is there something fundamentally wrong with what I am
considering here? Or perhaps there is a really trivial solution I am
overlooking?
Richard Forrest
classes need to return different iterator types (but with the same
value_types etc).
Classes A and B both conform to the same abstract Interface class. Interface
has a pair of virtual functions begin() and end() that generate a typical
STL style range. Classes A and B provide different implementations, maybe
using different types of container. The problem is that, to conform to the
interface, A and B must return the same type (or a derived type). They can
not do this directly as their containers (and hence their iterators) are of
different types.
class Interface
{
public:
virtual VirtualIterator begin() = 0;
virtual VirtualIterator end() = 0;
};
class A : public Interface
{
public:
virtual VirtualIterator begin() {return
make_virtual_iterator(container_.begin());}
virtual VirtualIterator end() {return
make_virtual_iterator(container_.end());}
private:
OneKindOfContainer container_;
};
class B : public Interface
{
public:
virtual VirtualIterator begin() {return
make_virtual_iterator(container_.begin());}
virtual VirtualIterator end() {return
make_virtual_iterator(container_.end())}
private:
AnotherKindOfContainer container_;
};
I think I see how to solve this problem by creating (as the example hints
at) a kind of VirtualIterator class that maintains a pointer to heap based
copy of the real iterator. But this issue must be fairly common and I would
much rather reuse a tried and tested solution than roll my own. I looked at
the boost iterator library but it does not seem to provide what I am looking
for. This made me a bit concerned because it seems to be very well thought
out and comprehensive. Is there something fundamentally wrong with what I am
considering here? Or perhaps there is a really trivial solution I am
overlooking?
Richard Forrest