implementing a stack with template
How would you write a header file to implement a stack of const char* pointers using the above template class?? thanks guys
Code:
#include <cassert>
#include <stdlib>
template<class Type> class TArray
{
Type* _data; // an array to store the data
int _size; // the number of elements in the array
int _capacity; // the number of spaces currently available
void growTo(int index)
{
if (index<_capacity)
return;
int newCapacity = _capacity?(2*_capacity):1;
if (newCapacity<=index)
newCapacity = index+1;
Type* newData = new Type[newCapacity];
for (int i = 0; i<size(); i++)
newData[i] = _data[i];
delete[] _data;
_data = newData;
_capacity = newCapacity;
}
public:
~TArray<Type>() { delete[] _data;}
TArray<Type>(unsigned int capacity = 0)
{
_size = 0;
_capacity = capacity;
if (capacity)
_data = new Type[capacity];
else
_data = 0;
}
int size() const { return _size;}
Type& operator[](int index)
{
assert(index>=0);
if (index>=_capacity)
growTo(index);
if (index>=_size)
_size = index+1;
return _data[index];
}
};