|
From: <ri...@te...> - 2003-02-22 23:25:19
|
Well if you don´t want to use STL you have two options:
1) Build your own vector class which can resize an array in run time.
That´s good for learning, but choosing this option means more writing
more code and since you already have the vector class in STL I think
it´s no point in it.
2) Write a function for resize an array:
Good option if you are using "old-school" arrays. A generic
function could be:
template <class T>
T* resize(& T* source_array, const int &source_array_size, const int
&new_n_elements, const T &inital_value = (T) 0 ){
//Allocate memory for new array;
T* temp = new T [new_n_elements];
//Copy the "old" array direction
T* temp" = source_array;
//Copy the source array
for(int i=0; (i < source_array_elements) && (i < new_n_elements); i++)
temp[i]=source_array[i];
//Init new values.
for (int i = source_array_size; i < new_n_elements; i++)
temp[i] = initial_value;
source_array = temp;
delete [] temp;
return source_array;
}
And, if you have an sprite array which you want to resize:
int size_array = 10;
SPRITE array[size];
resize (array[], size, size+1);
And last, one advice: don´t resize the array for just one element, it
would be better in little increments because of function overhead
A value between 5 - 10 is a good value for your SPRITE class IMHO.
Greetings.
|