From 5e5a34537e5f2efc71181f2bf6878c3e2e62b398 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Wed, 22 May 2013 23:02:00 +0300 Subject: [PATCH] Implement the missing assignment operator in CompositeShape Also rewrite the copy constructor in a slightly different way. --- source/geometry/compositeshape.h | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) 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 -- 2.45.2