c++ - Accessing derived objects in boost::ptr_vector -
i using boost::ptr_vector < class > , use store objects of class b : public class a. want able access class b objects in vector; how cast access?
ideally, a should provide virtual interface allows access parts of b need. if need access actual b objects, need use dynamic_cast on reference yielded iterator container (you use static_cast if knew certainty iterator pointed @ b object):
// create container , insert new element it: boost::ptr_vector<a> s; s.push_back(new b()); // reference element inserted: b& b_ref = dynamic_cast<b&>(*s.begin()); if wanted iterate on b elements in container (and skip on non-b elements), using combination of boost's transform_iterator (to convert each a& b&) , filter_iterator (to skip on non-b elements in container).
Comments
Post a Comment