template <typename T>
class Array2D
{
public:
// constructor
Array2D(unsigned wd,unsigned ht)
: nWd(wd), nHt(ht), pAr(0)
{
if(wd > 0 && ht > 0)
pAr = new T[wd * ht];
}
// destructor
~Array2D()
{
delete[] pAr;
}
// indexing (parenthesis operator)
// two of them (for const correctness)
const T& operator () (unsigned x,unsigned y) const
{ return pAr[ y*nWd + x ]; }
T& operator () (unsigned x,unsigned y)
{ return pAr[ y*nWd + x ]; }
// get dims
unsigned GetRows() const { return nWd; }
unsigned GetColns() const { return nHt; }
// private data members
private:
unsigned nWd;
unsigned nHt;
T* pAr;
// to prevent unwanted copying:
Array2D(const Array2D<T>&);
Array2D& operator = (const Array2D<T>&);
};
It wasn't written by me, but it works good. I am just starting to learn how to overload parameters.
Can you please help, and, as an example, create an overloaded "+" operator?