|
Boost Users : |
From: David Abrahams (dave_at_[hidden])
Date: 2004-03-22 17:16:50
Paul Elliott <pelliott_at_[hidden]> writes:
> I am trying to figure out iterators and I have a dumb question:
> I have an iterator A_iter, that can traverse in both directions
> ( A_iter++ and A_iter-- ) and can do input (operator*()).
> Value_type = type of *A_iter is A;
>
> and A is static_cast able to B:
>
> A start;
> B fish = static_cast<B>(start); // OK
>
> How do I use adapters to create B_iter, which is the same as A_iter,
> but whose value_type= typeof *B_iter is B?
>
> I am sorry if this is a dumb question.
Your question isn't dumb, but it's incomplete. It's not clear how
you want the B_iter to produce Bs from the As that A_iter yields.
The message subject indicates you intend to use dynamic_cast. I
assume if you care about dynamic_cast that B must be derived from A,
but if the type of *A_iter is A (and not A&), you won't be able to
dynamic_cast down to B. Same goes for static_cast, which might
compile but will produce undefined behavior from downcasting if you
start with an A.
Aside from those considerations, you should be able to simply use
transform_iterator with a function or function object that does the
desired cast.
-- 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