[英]calling method in template class in template member function of another class
我在如何处理SetOfCells
类的get
方法中返回的模板值时遇到麻烦。
是否可以按照以下所示方式进行? 正确的语法是什么? (我使用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
使用cellParent
它没有定义template <class T> T get(int cellIndex)
或它定义了它,但是在cell
类中没有被覆盖。
请注意,您无法做您想做的事情:您无法在C ++中覆盖模板成员函数。
因此,我的建议是让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.