0) Don't do this stuff yourself, except for practice. Use std::vector.
1) 'new' - at least as normally invoked - CANNOT return NULL. If there was a failure, it will throw an exception. Also, assertions shouldn't be used to detect runtime failures of that sort; they should be used in cases where you are pretty sure it's your fault if it ever gets tripped (i.e. they're a debugging tool).
2) Calling delete[] on a NULL pointer is well-defined, standard and safe: it does nothing. You should not include this check.
3) The postcondition feels awkward. Why not just keep the elements that will still fit? (This is what std::vector will do BTW)
4) You don't need to loop to copy elements. Use std::copy, as shown. But of course, you should be using std::vector anyway :)
5) Why allow the pointer to be NULL inside the class anyway? Why not just have it point at a zero-size allocation instead?
6) You don't need to static_cast the result of relational operators to bool. It already is bool. But you wouldn't need to static_cast it anyway, as long as it's convertible-to-bool.
//POST: If this array is shrunk, its contents are lost.void setSize(int _size) { assert(_size >= 0, "New array size negative.", true); assert(ptr); // fix the constructor to make sure it's always initialized to // point to some allocation. if (size == _size) { return; } // nothing to do. I* old = ptr; ptr = new I[_size]; std::copy(old, // at old + std::min(size, _size), // until ptr); // to delete[] old; size = _size;}
7) The erroneous code appears to be here:
// concatenate two objects?int temp=size;setSize(size+other.size);for(int i=temp;i<size;++i) ptr=other.ptr; // ERROR! wrong indices into 'other'!
You presumably want '=other.ptr
'.<br><br>8) Don't do this stuff yourself, except for practice. Use std::vector. Incidentally, it has a smarter way of handling the memory allocations, to avoid having to re-allocate for every appended element.