c++ - Std::vector to const std::vector* -
c++ - Std::vector<struct> to const std::vector<const struct>* -
i have private variable std::vector<some struct>
can somehow pass vector function homecoming value, no "write" access, not able add together new elements it, , won't able modify elements(e.g. const std::vector<some const struct>*
. how can that? thought have create new vector constant pointers existing elements. there improve solution?
you can't modify elements of vector via pointer or reference const
vector. next safe:
const std::vector<some_type>* get_pstuff() const { homecoming &the_vector; } const std::vector<some_type>& get_rstuff() const { homecoming the_vector; }
on other hand, might more idiomatic homecoming const_iterators begin , end of vector:
std::vector<some_type>::const_iterator cbegin() const { homecoming the_vector.cbegin(); } std::vector<some_type>::const_iterator cend() const { homecoming the_vector.cend(); }
c++ vector
Comments
Post a Comment