简体   繁体   English

指定类型时继承C ++中的抽象模板类

[英]Inheriting an Abstract Template Class in C++ while specifying the type

Say I have a template, abstract class: 说我有一个模板,抽象类:

template <class T>
class MyClass {
public:
    virtual bool foo(const T a, const T b) = 0;
}

And another class that wants to inherit, while getting rid of the template: 还有一个想要继承的类,同时摆脱了模板:

class MyInheritor : public MyClass<int *> {
public:
    bool foo(const int* a, const int* b) { /* stuff */ }
}

The above doesn't let me instantiate MyInheritor, saying it's an abstract class. 上面没有让我实例化MyInheritor,说它是一个抽象类。 How can I override the pure virtual method? 如何覆盖纯虚拟方法?

Because you are not overriding anything here: the const is applied to the int and not to the pointer to int. 因为您在这里没有覆盖任何内容:const应用于int而不应用于int的指针。

Here is the fixed version you probably want: 这是您可能想要的固定版本:

class MyInheritorFixed : public MyClass<int *> {
    bool foo(int* const a, int* const b) override { return true; }
};

Live 生活

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

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