简体   繁体   中英

calling method in template class in template member function of another class

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;
};

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;
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM