Array::Array (int _length)//the length could be 1!
{
if (_length > 0) {
length = _length;
ptr = newint[length];
for (int i = 0; i < length; i++)
ptr[i] = 0;
}
}
Array::~Array()
{
if(ptr != NULL){//I am not sure if this control has sense
if(length == 1) delete ptr;
//Is this correct?
//Or we dont't have to control if length == 1 and just delete[] ptr?
elsedelete [] ptr;
}
}
In your constructor, why are you checking the value of length? The constructor is called when you're instantiating an object, and so you haven't done anything to it before. It could contain garbage or some negative number that will cause your code to not execute at all.