Implementing pure virtual function from abstract base class: does override specifier have any meaning?

前端 未结 4 412
执笔经年
执笔经年 2021-01-19 10:20

Background

I just stumbled over a use case of the override specifier that, as far as I can tell, seems redundant and also without any particular semantics meaning,

4条回答
  •  时光说笑
    2021-01-19 10:47

    I'm not a big fan of override, but, assuming it's something that you find useful in general, then, yes, putting override on a virtual function that overrides a pure virtual functions is useful. Consider this rather contrived example:

    struct Base {
        virtual void f() = 0;
    };
    
    struct Derived : Base {
        virtual void f();
        virtual void f(int);
    };
    

    Now suppose that the maintainer of Base (perhaps even your future self) changes Base to look like this:

    struct Base {
        virtual void f(int) = 0;
    };
    

    Now the behavior of Derived has quietly changed. With override the compiler would report an error.

提交回复
热议问题