假设我有一个模板,抽象类:
template <class T>
class MyClass {
public:
virtual bool foo(const T a, const T b) = 0;
}
另一个类想要继承,同时摆脱模板:
class MyInheritor : public MyClass<int *> {
public:
bool foo(const int* a, const int* b) { /* stuff */ }
}
上面不允许我实例化 MyInheritor,说它是一个抽象类。如何重写纯虚方法?
最佳答案
因为您没有在这里重写任何内容:const 应用于 int 而不是指向 int 的指针。
这是您可能想要的固定版本:
class MyInheritorFixed : public MyClass<int *> {
bool foo(int* const a, int* const b) override { return true; }
};
关于c++ - 在 C++ 中继承抽象模板类并指定类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33880589/