简体   繁体   中英

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. 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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