lately in my object oriented programming class we were dealing with templates.
in a question we got, we were asked to create a Queue class that can store any type
now my problem start when i want to store an array of somesort in this queue, for example:
Queue < char* >
now when i want to insert a new "node" to the queue i dont want to create a double pointing to a memory block. so basicly my question is: "how can i create an array of the same type of what the template class is pointing at?"
template<class T>
void Queue::enQueue(const T& value, int size = 1)
{
//exeptions handaling...
//handaling the case in wich the template is a pointer
if( _Is_pointer<T>() == true )
{
T temp = new T[size]; // i know its a mistake but thats what i basicly mean to do
for(int i = 0; i < size; i++)
temp[i] = value[i];
m_arr[++m_occupied] = temp; // m_arr is a data member of the T objects, m_occupied is as the name suggest
}
//...
}
thanks for the help :)
char*is a pointer, not an array.Queue<char *>, why not respect this and give me what I would expect? Why are you writing special code, just because the type I passed to you is a pointer?Queue<char*>, let them worry about thechar*and what it potentially points to. All your Queue class should worry about is its own memory, not the user's memory. For example, look atstd::vector-- if someone wants astd::vector<char*>, vector does nothing special just because the type is a pointer. If you're writing special code just because the type is a pointer, then your implementation is flawed.