Original Post
Hello. I started writing a dynamic array class, mostly as a learning experience (but also for possible future use). As it's how Flavian Brebion is doing it for Infinity, I'm using the C memory management functions. I thank him for his assistance. So far it looks like this: It's incomplete. Anyway, at first I couldn't figure out how to destruct elements; integral types don't require destruction (I think?) while objects do. I tried this on a whim: and it surprisingly compiled without error. Is that syntax correct? Also, if you have any suggestions based on what I've shown, please share them. There's relatively little information out there on writing container classes.
template <typename _type> class DynamicArray
{
public:
DynamicArray<_type>(unsigned int reservedMemory = 0);
~DynamicArray();
void append(const _type& element);
_type operator[](int params){return *(arrayLocation+params);};
protected:
size_t objectSize;
_type* arrayLocation;
unsigned int maxSize;
unsigned int numElements;
};
template <typename _type> DynamicArray<_type>::DynamicArray(unsigned int reservedMemory) :
objectSize(sizeof(_type)),
arrayLocation((_type*) malloc(reservedMemory * objectSize)),
maxSize(reservedMemory),
numElements(0)
{}
template <typename _type> void DynamicArray< _type >::append(const _type& element)
{
if(numElements >= maxSize)
{
arrayLocation = (_type*) realloc((void*)arrayLocation, (numElements+1)*objectSize);
maxSize+=1;
}
new(arrayLocation + numElements) _type(element);
numElements++;
}
template <typename _type> DynamicArray<_type>::~DynamicArray()
{
_type* arrayLocationCopy = arrayLocation;
for(unsigned int x = 0; x < numElements; x++)
{
arrayLocationCopy->~_type();
arrayLocationCopy++;
};
free(arrayLocation);
}
~_type();