I have the following code:
vector<C1*>::iterator itr = vec.begin(); for (; itr != vec.end(); ++itr) { C2 *c = dynamic_cast<C2*>(*itr); c->f(); }
I am wondering if I can use single line for_each to replace it. I tried the following:
for_each(vec.begin(), vec.end(), bind2nd(mem_fun(&C2::f), dynamic_cast<C2*>));
But I get a compilation error,
expected unqualified-id before 'dynamic_cast'
What should be right?
[EDIT] I can't use C ++ 11. It looks like I should define an extra functor, sigh.
For some questions about the code itself: C1 and C2 - 2 clean interfaces; The f () function is only available as a C2 API. The "vec" vector has a list of objects that have both C1 and C2 interfaces, but they are passed to this code fragment as C1 * vector.
c ++ foreach dynamic-cast
my_question
source share