Ben said:
As said, you can't cast.
You'll have to copy each element.
You have to copy each element if you want a vector< const Foo * >
However if all you want to do is pass a reference to your vector and
ensure that none of the Foo objects are modified, you can use an
adapter,
template < typename T >
vector_const_adapter
{
typedef std::vector< T * > vec_type;
const vec_type * itsVecRef;
public:
typedef typename vec_type::size_type size_type;
vector_const_adapter() : itsVecRef( 0 ) {}
vector_const_adapter( const vec_type & vecRef )
: itsVecRef( &vecRef )
{
}
const T* operator[] ( size_type idx ) const
{
return (*itsVecRef)[ idx ];
}
bool empty() const;
size_type size() const;
const T* const * begin() const;
const T* const * end() const;
};
and I'll leave it up to you to implement the functions empty() size(),
begin() and end()
Now all you have to do is pass around vector_const_adapter<Foo>. Note I
used a member pointer so that vector_const_adapter can have a default
constructor and be assignable. If you don't want these features use a
member reference.