Boost logo

Boost Users :

From: David Abrahams (dave_at_[hidden])
Date: 2005-12-03 18:44:49


Hugh Hoover <hugh_at_[hidden]> writes:

> I have a need to provide STL compliant iterators from an abstract
> class A, that is, without knowing the runtime type of the actual
> "collection" being iterated.. As near as I can tell, these things
> don't mix well. My best so far is to create an abstract
> "iterator_assister" which provides dereference, (in|de)crement and
> is_equal, and use a boost::iterator_adaptor derived class to provide
> the iterator the client code uses, and allocate the iterator_assister
> derived class from the implementation class (derived from A) that's
> being iterated over.

Have you looked at boost::indirect_iterator? Just make a container of
pointers to the abstract base and iterate over that :)

> This solution has obvious downsides. I can't imagine that this is a
> unique problem. Certainly, people significantly smarter than me have
> figured out a good solution to this? In my reading and browsing,
> though, I've never seen the problem presented, never mind solved. In
> my early C++ days, we used allocated iterators rather than the
> current STL style.
>
> Can anyone provide pointers to what I SHOULD be reading for a good
> solution?
>
> I believe I NEED to use abstract classes here rather than meta-
> programming with known types at runtime - the client code will need
> to iterate over classes loaded from dynamic libraries that are not
> known to the client code (except by abstract interface).

There are sometimes interesting ways to move the boundary between
static and dynamic polymorphism around that can solve problems like
this one.
http://boost-consulting.com/slides/connections05/Life%20On%20the%20Edge.ppt
might be of some use to you.

-- 
Dave Abrahams
Boost Consulting
www.boost-consulting.com

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net