1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
|
#include "intarray.ih"
IntArray::IntArray(size_t size)
:
d_size(size)
{
if (d_size < 1)
throw string("IntArray: size of array must be >= 1");
d_data = new int[d_size];
}
IntArray::IntArray(IntArray const &other)
:
d_size(other.d_size),
d_data(new int[d_size])
{
memcpy(d_data, other.d_data, d_size * sizeof(int));
}
IntArray::~IntArray()
{
delete[] d_data;
}
IntArray const &IntArray::operator=(IntArray const &other)
{
IntArray tmp(other);
swap(tmp);
return *this;
}
int &IntArray::operatorIndex(size_t index) const
{
boundary(index);
return d_data[index];
}
int &IntArray::operator[](size_t index)
{
return operatorIndex(index);
}
int const &IntArray::operator[](size_t index) const
{
return operatorIndex(index);
}
void IntArray::boundary(size_t index) const
{
if (index < d_size)
return;
ostringstream out;
out << "IntArray: boundary overflow, index = " <<
index << ", should be < " << d_size << '\n';
throw out.str();
}
|