Frage

I am having trouble on how to handle the template value return in the get method of the SetOfCells class.

Is it possible to do it in the way shown below? What is the correct Syntax for doing this? ( I use the cellParent pointer array to point to each cells)

template <class T>
class cell : public cellParent 
{
    .....
    T get() { return Val;}
    .....
private:
    T val;
};

class SetOfCells
{
    ....
    template<class T> T get(int cellIndex)
    {
       return cellArray[cellIndex]->get();
    }
    ....
private:
    cellParent**  cellArray;
};
War es hilfreich?

Lösung

SetOfCells uses cellParent - which either does not define template <class T> T get(int cellIndex) or it defines it but it is not overridden in the cell class.

Note it is not possible to do what you are trying to do: you cannot override a template member function in C++.

So, my suggestion would be to have SetOfCells be a template class and have a cell<T>** member.

template <class T>
class SetOfCells
{
    ....
    T get(int cellIndex)
    {
       return cellArray[cellIndex]->get();
    }
    ....
private:
    cell<T>**  cellArray;
};
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top