I have a templated array class and I want to write an init routine for it but I'm not sure how to do that since I don't pass it any particular data type. Any ideas?
Michael:
confused:
I have a templated array class and I want to write an init routine for it but I'm not sure how to do that since I don't pass it any particular data type. Any ideas?
Michael:
confused:
What do you intending on doing in you 'init' routine?
I want to initialize all the elements to zero or an equivalent for a template....
Your array class shouldn't have to worry about how each type represents 0. The type (whatever it may be) should implement that in a way which is invisible to outsiders (eg. Default Constructor)
You do it the same way as if the array was not templated, however, I recommend that if you are talking about initalizing the ELEMENTS to 0 by default, then think otherwise, because while that would be fine for primitive datatypes, all of the user-made datatypes that you, or other people, create, must have an implicit constructor that takes the single parameter 0, which is pretty limiting.
Anyways, the syntax for initialization is exactly the same, I'm not sure where your problem is.
So let's say my template class is this...
template <class T>
class RArray {
private:
typedef enum { StartArraySize = 128} bogus;
typedef enum {InitialSize = 0};
size_t num_elem;
size_t array_size;
size_t array_Cursor;
T *pArray;
private:
public:
I can add an init routine like this?
RArray()
{
pArray = new T[ StartArraySize ];
num_elem = InitialSize;
array_Cursor = InitialSize;
array_size = StartArraySize;
}
void Init()
{
int i = 0;
for (;i< StartArraySize;i++)
{
pArray[i] = 0;
}
pArray
}
~RArray()
{
if (pArray != NULL) {
delete [] pArray;
}
}
Last edited by adonisv; 02-12-2003 at 06:54 PM.