Quote:Original post by ConorH
DynArray.h
*** Source Snippet Removed ***
DynArray.cpp
*** Source Snippet Removed ***
You class is flawed in many places. I'm going to review it line by line, for the moment try this:
// place this all in dynarray.h#include <vector>template <class T>class DynArray {public: //Ctors DynArray(){} DynArray(int i):vec(i){} //Dtors //~DynArray(); //Member funcs void Add(const T &t) { vec.push_back(t); } void Insert(int ind, const T &val) { vec.insert(vec.begin() + ind, val); } void Remove(int ind) { vec.erase(vec.begin() + ind ); } void Empty() { vec.clear(); } void Set(int ind,const T &val) { vec.at(ind) = val; }/* removed due to unclear functionality int Find(int ind); int FindNext(int ind);*/ //Setters void SetSize(int sz) { vec.resize(sz); } //Getters int GetSize() const { return vec.size(); } int UpperB() const { return GetSize() - 1; } //Operators T& operator[](int index) {return vec[index];} const T& operator[] const(int index) {return vec[index];} private: std::vector<T> vec;};
As you can see std::vector neatly supports all the operations you want to use. You should consider using it directly in your sprite class.
Let us consider some of the things that are wrong with your class.
1) memcpy. Don't use memcpy, it cannot work on non-POD objects. Always use std::copy.
2) When you add objects to the array, it reallocates the internal array every time. This is extremely costly. Consider a strategy like std::vector, overallocation.
3) Empty(). This makes no sense? Why have a function to set all the values to 0?
I was expecting something similar to either bool std::vector::empty() const; which tells me if Size == 0, or void std::vector::clear(); which would clear all the elements from the dynamic array.
4) Perhaps Set() should signal the out of bounds access as an error (possibly via an assert, or an exception) rather than silently ignoring it.
5) GetSize() and GetUpperB() should be const.
6) You should provide "const T &operator[]( int ) const;" too.
7) No copy constructor or assignment operator. Follow the "rule of three". If you need any of {destructor,copy constructor, operator=}, then you probably need all 3.