我想从模板类继承并更改调用运算符“()”时的行为 - 我想调用另一个函数。这段代码
template<typename T>
class InsertItem
{
protected:
int counter;
T destination;
public:
virtual void operator()(std::string item) {
destination->Insert(item.c_str(), counter++);
}
public:
InsertItem(T argDestination) {
counter= 0;
destination = argDestination;
}
};
template<typename T>
class InsertItem2 : InsertItem
{
public:
virtual void operator()(std::string item) {
destination ->Insert2(item.c_str(), counter++, 0);
}
};
给我这个错误:
Error 1 error C2955: 'InsertItem' : use of class template requires template argument list...
我想问您如何正确执行此操作,或者是否有其他方法可以执行此操作。谢谢。
最佳答案
继承时必须说明如何实例化父模板,如果可以使用相同的模板类 T,请这样做:
template<typename T>
class InsertItem
{
protected:
int counter;
T destination;
public:
virtual void operator()(std::string item) {
destination->Insert(item.c_str(), counter++);
}
public:
InsertItem(T argDestination) {
counter= 0;
destination = argDestination;
}
};
template<typename T>
class InsertItem2 : InsertItem<T>
{
public:
virtual void operator()(std::string item) {
destination ->Insert2(item.c_str(), counter++, 0);
}
};
如果需要其他内容,只需更改行:
class InsertItem2 : InsertItem<needed template type here>
关于c++ - 类模板继承 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12895775/