2

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 :)

5
  • A char* is a pointer, not an array. Commented Jan 17, 2015 at 20:50
  • well i mean allocating one by a pointer, and its not necceraliy a 'char*' i mean any pointer Commented Jan 17, 2015 at 20:54
  • @segal Why do you differentiate between whether or not a pointer is passed? If I want a 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? Commented Jan 17, 2015 at 20:57
  • well i do that because if i enqueue a ` char* ` for example and i delete it @PaulMcKenzie from the main i dont want to create double pointing, because when this queue will reach its d'tor ill basicly try to release memory i dont own anymore, though its true that i can do that to basiclly anything when i think about it now. ive down it after i wrote the code and done some stuff so i thought about all this thing afterwards Commented Jan 17, 2015 at 21:03
  • @segal - You're doing too much "hand-holding". If someone wants a Queue<char*>, let them worry about the char* 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 at std::vector -- if someone wants a std::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. Commented Jan 18, 2015 at 4:03

1 Answer 1

2

You could make template argument deduction work for you

// handling the case in wich the template is a pointer
template <class T> void Queue::enQueue(T const* value, int size = 1) {

This way, the overload deduces T as the type of object that value points at.

Now, you probably want to std::vector because you cannot treat arrays as simple values. Also, the use of new and delete for this kind of task is a code smell.

Guideline: In modern c++, vector<> is the default container for dynamically sized arrays, array<> for fixed-size arrays.

// handling the case in wich the template is a pointer
template <class T> void Queue::enQueue(T const* value, int size = 1) {
    m_arr[++m_occupied] = temp(value, value + size);
}

BONUS You can even deduce arrays with size, if you're passing true references to arrays:

// handling the case in wich the template is an array reference
template <class T, size_t Size> void Queue::enQueue(T const (&value)[Size]) {
    m_arr[++m_occupied] = std::vector<T>(value, value + Size);
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.