buenas a todos
me gustaria compartiles algo que estoy haciendo para aprender mas sobre los punteros y los templates, antes que nada se que podria usar una lista enlazada y podria evitarme lo de asignarle memoria dinamica con new pero quiero tenerla asi controlada con tamano fijo.
me gustaria sus opiniones.
template<class T>
class FixedArrayTemplate
{
private :
int max_size;
int size;
T *Allocate_array;
bool Correct;
public:
FixedArrayTemplate(const unsigned int _size)
{
Correct = CreateMemoryArray(_size);
}
bool Push(T element)
{
if(size < max_size)
{
Allocate_array[size] = element;
size++;
return true;
}
else
return false;
}
bool isCorrect(){return Correct;}
void Clean()
{
if(this->Correct) delete[] this->Allocate_array;
this->Allocate_array = NULL;
this->Correct = false;
size = 0;
max_size = 0;
}
bool CreateMemoryArray(int _size)
{
if(this->isCorrect())
this->Clean();
max_size = _size;
size = 0;
Allocate_array = new T[_size];
if(Allocate_array == 0)
{
Correct = false;
return Correct;
}
else
{
Correct = true;
return Correct;
}
}
T getElement(int e)
{
if(( e > 0 && e < size ) && e < this->max_size )
{
if(this->Correct)
return this->Allocate_array[e - 1];
}
else
{
if(this->Correct)
return this->Allocate_array[size - 1];
}
}
~FixedArrayTemplate()
{
Clean();
}
};
int main()
{
char *buffer1 ="buffer 1 ";
char *buffer2 = "buffer 2";
char *buffer3 = "buffer 3";
char *buffer4 = "buffer 4";
char *buffer5 = "buffer 5";
FixedArrayTemplate<char *>miArray(20);
if(miArray.isCorrect())
{
miArray.Push(buffer1);
miArray.Push(buffer2);
miArray.Push(buffer);
miArray.Push(buffer3);
miArray.Push(buffer4);
miArray.Push(buffer5);
cout << miArray.getElement(0)<<endl;
cout << miArray.getElement(1)<<endl;
cout << miArray.getElement(2)<<endl;
cout << miArray.getElement(3)<<endl;
cout << miArray.getElement(4)<<endl;
cout << miArray.getElement(5)<<endl;
cout << miArray.getElement(6)<<endl;
}
}
gracias su opinion es muy importante.