[ create a new paste ] login | about

Link: http://codepad.org/klDWMowB    [ raw code | fork ]

C++, pasted on Dec 29:
#ifndef CARRAY_HPP
#define CARRAY_HPP

template <typename Data>
class Array {
  public:
    Array();
    Array(int size);
    virtual ~Array();
    const int size() const;
    Data& operator[](int pos);
    Array<Data> operator=(const Array<Data>& other);
  
  private:
    int mSize;
    Data* mData;
};

#endif // CARRAY_HPP

#include "CArray.hpp"
#include <stdlib.h>

template <typename Data>
Array<Data>::Array(int size):mSize(abs(size)) {
  if (0<mSize)
    mData=new Data[mSize];
}

template <typename Data>
Array<Data>::Array():mSize(0) {}

template <typename Data>
Array< Data > Array< Data >::operator=(const Array< Data >& other) {
  if (this!=&other) {
    mSize=other.size();
    mData=new Data[mSize];
    for (int i=0;i<mSize;i++)
      mData[i]=other[i];
  }
  return *this;
}

template <typename Data>
Data& Array< Data >::operator[](int pos) {
  Data* ans=0;
  if ((-1<pos) && (mSize>pos))
    ans=mData+pos;
  return ans;
}

template <typename Data>
const int Array<Data>::size() const {
  return mSize;
}

template <typename Data>
Array<Data>::~Array() {
  if (mData)
    delete mData;
}


Create a new paste based on this one


Comments: