Possible Duplicate:
Is it ok to cast a STL container with Base type to Derived type?
This should be a quick question… If I have a container, say an STL list, of a base class, is it possible to casts the entire container to a type of subclass? E.g.
[A inherits from base class B]
list<B*> list1;
list1.push_back(new A());
list<A*> list2 = list1;
The compiler is complaining: “conversion from std::vector<B*, std:allocator<B*> >' to non-scalar type 'std::vector<Bar*, ...>' requested.“
Is this possible, or am I’m just screwing it up?
No.
std::list<T>andstd::list<U>are completely different, incompatible types, ifstd::is_same<T,U>::valueisfalse, i.e if the type argumentsTandUaren’t same.Use
std::transformand do appropriate casting in the callback.