vector<Base*>
and vector<Derived*>
are unrelated types, so you can't do this. This is explained in the C++ FAQ here.
You could change your variable from a vector<Derived*>
to a vector<Base*>
and insert Derived
objects into it.
Also, you should pass the vector by const-reference, not by value:
void BaseFoo( const std::vector<Base*>& vec )
{
...
}
Finally, to avoid memory leaks, and make your code exception-safe, consider using a container designed to handle heap-allocated objects, e.g:
#include <boost/ptr_container/ptr_vector.hpp>
boost::ptr_vector<Base> vec;
Alternatively, change the vector to hold a smart pointer instead of using raw pointers:
#include <memory>
std::vector< std::tr1::shared_ptr<Base*> > vec;
or
#include <boost/shared_ptr.hpp>
std::vector< boost::shared_ptr<Base*> > vec;
In each case, you would need to modify your BaseFoo
function accordingly.