简体   繁体   English

另一个类的模板成员函数中的模板类中的调用方法

[英]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. 我在如何处理SetOfCells类的get方法中返回的模板值时遇到麻烦。

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) (我使用cellParent指针数组指向每个单元格)

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. SetOfCells使用cellParent它没有定义template <class T> T get(int cellIndex)或它定义了它,但是在cell类中没有被覆盖。

Note it is not possible to do what you are trying to do: you cannot override a template member function in C++. 请注意,您无法做您想做的事情:您无法在C ++中覆盖模板成员函数。

So, my suggestion would be to have SetOfCells be a template class and have a cell<T>** member. 因此,我的建议是让SetOfCells作为模板类,并具有cell<T>**成员。

template <class T>
class SetOfCells
{
    ....
    T get(int cellIndex)
    {
       return cellArray[cellIndex]->get();
    }
    ....
private:
    cell<T>**  cellArray;
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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