
Daniel Lidström <daniel.lidstrom@sbg.se> writes:
Now to my problem: A PxyPoint is a sub class to DataObject, and PxyFileData::GetPoints() returns a DataObject where all the children are PxyPoint's. Now I would like to call a member function, PxyPoint::PutSpCode(const char*), on each of the children, like this:
void put_sp_code(DataObject&, const char*);
void test() { shared_ptr<PxyFileData> file(...);
DataObject& points = file->GetPoints(); put_sp_code(points, "spcode"); }
void put_sp_code(DataObject& points, const char* code) { std::for_each(points.begin(), points.end(), std::bind2nd(std::mem_fun(&PxyPoint::PutSpCode), code));
If PutSpCode is a virtual function on DataObjects, it's easy. // Using Boost.Bind because the standard binders are too tricky for // me to get right (untested); std::for_each(points.begin(), points.end(), boost::bind(&DataObject::PutSpCode, _1, code)); Otherwise I don't think you could reasonably expect anything like this to work without a cast. You'd need to create a function object with a cast in it: // using Boost.Lambda (untested) std::for_each(points.begin(), points.end(), (&boost::ll_dynamic_cast<PxyPoint&>(_1))->*(&PxyPoint::PutSpCode)(code)); or std::for_each(points.begin(), points.end(), (&boost::ll_static_cast<PxyPoint&>(_1))->*(&PxyPoint::PutSpCode)(code));
Is this possible with our design? I would like to be able to do this without having to create a PxyPointList, that derives from DataObject and only stores PxyPoint's (my guess is that something like that would work with this code).
I can't tell for sure, but this has all the markings of a design that's trying to use runtime polymorphism where it should be using compile-time polymorphism, in some form or other. BTW, what does any of this have to do with iterator_facade? -- Dave Abrahams Boost Consulting www.boost-consulting.com