, :
#include <iostream>
struct base
{
virtual ~base(void) {}
virtual void print(std::ostream& pStream) = 0;
};
struct foo : base
{
void print(std::ostream& pStream) { pStream << "foo" << std::endl; }
};
struct bar : base
{
void print(std::ostream& pStream) { pStream << "bar" << std::endl; }
};
#include <boost/bind.hpp>
#include <boost/ptr_container/ptr_vector.hpp>
#include <algorithm>
int main(void)
{
boost::ptr_vector<base> v;
v.push_back(new foo);
v.push_back(new bar);
std::for_each(v.begin(), v.end(),
boost::bind(&base::print, _1, boost::ref(std::cout)));
}
-, boost, ptr_vector . , .
-, , ; boost::bind . boost::reference_wrapper ( boost::ref), . , , .
( boost::ref .)
, BOOST_FOREACH, , , :
#include <boost/foreach.hpp>
#include <boost/ptr_container/ptr_vector.hpp>
#include <algorithm>
#define foreach BOOST_FOREACH
int main(void)
{
boost::ptr_vector<base> v;
v.push_back(new foo);
v.push_back(new bar);
foreach (base* b, v)
{
v->print(std::cout);
}
}