Panjandrum wrote:
Jeff Flinn wrote: See http://www.boost.org/libs/assign/doc/index.html
which allows you to:
vector<Shape*> shape_vec = List_of(&triangle)(&square};
And what is the advantage compared to James Daughtry's clean and
simple solution?
Well unless my eyes deceive me, the above is accomplished in a single
statement, which is my understanding as to what the OP desired. Also the
boost assignment library allows use in initializers:
class CompoundShape
{
std::vector<Shape*> mShapes;
public:
CompoundShape()
: mShapes( List_of(new Triangle)(new Square) )
{}
};
Although both 'Shape* init[] = {new Triangle, new Square};' and the above
usage may have exception safety issues. What if either Triangle or Square
constructors throw. I'm not sure if even defining 'std::vector<
boost::shared_ptr<Shape> > mShapes;' is sufficient.
Jeff Flinn