From: Mikko Rasa Date: Wed, 22 May 2013 20:02:00 +0000 (+0300) Subject: Implement the missing assignment operator in CompositeShape X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=5e5a34537e5f2efc71181f2bf6878c3e2e62b398;p=libs%2Fmath.git Implement the missing assignment operator in CompositeShape Also rewrite the copy constructor in a slightly different way. --- diff --git a/source/geometry/compositeshape.h b/source/geometry/compositeshape.h index 8466076..55467d7 100644 --- a/source/geometry/compositeshape.h +++ b/source/geometry/compositeshape.h @@ -22,10 +22,10 @@ protected: CompositeShape() { } CompositeShape(const Shape &, const Shape &); - CompositeShape(const CompositeShape &); - CompositeShape &operator=(const CompositeShape &); template void init_from_iter_range(const Iter &, const Iter &); + CompositeShape(const CompositeShape &); + CompositeShape &operator=(const CompositeShape &); public: virtual ~CompositeShape(); @@ -55,11 +55,22 @@ inline void CompositeShape::init_from_iter_range(const Iter &begin, con } template -inline CompositeShape::CompositeShape(const CompositeShape &other) +inline CompositeShape::CompositeShape(const CompositeShape &other): + shapes(other.shapes) { - shapes.reserve(other.shapes.size()); - for(typename ShapeArray::const_iterator i=other.shapes.begin(); i!=other.shapes.end(); ++i) - shapes.push_back((*i)->clone()); + for(typename ShapeArray::iterator i=shapes.begin(); i!=shapes.end(); ++i) + *i = (*i)->clone(); +} + +template +inline CompositeShape &CompositeShape::operator=(const CompositeShape &other) +{ + for(typename ShapeArray::iterator i=shapes.begin(); i!=shapes.end(); ++i) + delete *i; + + shapes = other.shapes; + for(typename ShapeArray::iterator i=shapes.begin(); i!=shapes.end(); ++i) + *i = (*i)->clone(); } template