c++ - 在 C++ 中继承抽象模板类并指定类型

标签 c++ templates inheritance

假设我有一个模板,抽象类:

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

Live

关于c++ - 在 C++ 中继承抽象模板类并指定类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33880589/

相关文章:

java - 数组和 vector 的内存分配如何工作?

C++ 时间操作函数

c++ - 如何声明模板常量类型?

c++ - 什么时候必须声明模板中使用的函数?

javascript - 斯托扬·斯特凡诺夫 : JavaScript Patterns - "The Default Pattern"

objective-c - 如何在 Objective-C 中正确地子类化委托(delegate)属性?

c++ - 动态分配内存,拷贝构造函数出错

c++ - 偏专业的模板啦

css - 奇怪的从 "a"中的 "figure"元素继承到 "p"元素

c++ - 发现用户是否具有管理员权限