]> git.tdb.fi Git - libs/math.git/commitdiff
Implement the missing assignment operator in CompositeShape
authorMikko Rasa <tdb@tdb.fi>
Wed, 22 May 2013 20:02:00 +0000 (23:02 +0300)
committerMikko Rasa <tdb@tdb.fi>
Wed, 22 May 2013 20:02:00 +0000 (23:02 +0300)
Also rewrite the copy constructor in a slightly different way.

source/geometry/compositeshape.h

index 846607689df582a8a9f0ae46ae79c75eb832ed81..55467d762993add17cbc4fe74ae7bfda2f776c22 100644 (file)
@@ -22,10 +22,10 @@ protected:
 
        CompositeShape() { }
        CompositeShape(const Shape<T, D> &, const Shape<T, D> &);
-       CompositeShape(const CompositeShape &);
-       CompositeShape &operator=(const CompositeShape &);
        template<typename Iter>
        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<T, D, O>::init_from_iter_range(const Iter &begin, con
 }
 
 template<typename T, unsigned D, typename O>
-inline CompositeShape<T, D, O>::CompositeShape(const CompositeShape<T, D, O> &other)
+inline CompositeShape<T, D, O>::CompositeShape(const CompositeShape<T, D, O> &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<typename T, unsigned D, typename O>
+inline CompositeShape<T, D, O> &CompositeShape<T, D, O>::operator=(const CompositeShape<T, D, O> &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<typename T, unsigned D, typename O>