0

I have the following code:

template <typename T>
class A {
public:
    virtual void func() {
        // Things that don't use T parameter.
    }
};

class B : public A<int> {
public:
    void func() {
        A<int>::func();
        // Working...
    }
};

Basically, I want to override a virtual function defined in a class template from inside a class that inherits from a specialized version of such template (A).

Does this work in C++? Is it good practice? Also, is it ok to call the overridden method as I did in the overriding one? (please, note that, for compatibility with the toolchain I'm using, this is C++98)

Thank you.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
fb9
  • 37
  • 5
  • 2
    Yes you can. Do you have any specific problems with that? – πάντα ῥεῖ Oct 29 '20 at 18:37
  • 2
    `A::func();` can be shortened to `A::func();`, otherwise looks ok. – HolyBlackCat Oct 29 '20 at 18:39
  • @πάνταῥεῖ Well, it compiles, but the IDE doesn't see it as an override (it doesn't add the override symbols near the declarations of the overriding methods) and I don't understand why. Usually it signals overrides when I make them. But being Eclipse CDT I couldn't understand if its a limit of the tool or something done wrong by me. – fb9 Oct 29 '20 at 18:41
  • 1
    @fb9 Could well be an IDE only problem, it's perfectly OK to do so. – πάντα ῥεῖ Oct 29 '20 at 18:43
  • 2
    Most IDEs are somewhat dumb. – HolyBlackCat Oct 29 '20 at 18:45
  • @HolyBlackCat Thanks. I'm quite a newbie when it comes to templates. – fb9 Oct 29 '20 at 18:48
  • @πάνταῥεῖ Thanks for clarifying. Unfortunately I must use Eclipse for this job due to management decisions... I guess I will get used to this kind of things. – fb9 Oct 29 '20 at 18:51
  • 1
    @fb9 Sometimes Eclipse CDT has problems with the _"intellisense"_, yes. I've been working with it several years, and got used to it. In the end if the compiler says it's OK, you can just ignore these glitches most of the time. – πάντα ῥεῖ Oct 29 '20 at 18:55
  • 1
    its not quite clear why you think it matters whether `func` uses `T` or not. In `A::func` the template paramter `T` is `int`, so you are bascially worrying about `func` using `int` or not. Your code is fine also when `func` does use `T` – 463035818_is_not_an_ai Oct 29 '20 at 19:13
  • @idclev463035818 Well, I wrote it to give more context to my question, I didn't know if it was important or not. As I said, I'm not quite familiar with templates. That's why I ask on SO, it helps me in connecting the dots by asking to someone more experienced than me. Thank you for your comment. – fb9 Oct 29 '20 at 20:18

0 Answers0