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

前端 未结 4 413
执笔经年
执笔经年 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.

    0 讨论(0)
  • 2021-01-19 10:55

    Technically, both versions are syntactically correct and legal. The override specifier is mainly meant to prevent unexpected behavior. The compiler will throw an error as soon as it encounters a member function marked as override which is not actually overriding a virtual function. This may occur if, for some reason, you change the signature of the virtual base class function. Consider this example:

    class Abstract {
        virtual void foo() { ...}
    }; 
    
    class Derived : public Abstract {
        void foo() override { ... }
    };
    

    Now, if the signature of Abstract::foo is changed, let's say to

    class Abstract {
        virtual void foo(int bar) { ...}
    }; 
    

    the compiler will throw an error at Derived::foo as it no longer overrides a function of Abstract which it wouldn't without the override qualifier. This helps you to better maintain your code. However, in your specific case (i.e. with pure virtual declarations), an error would be thrown as well. So using override is mainly considered "good practice", I guess. More on that topic: http://en.cppreference.com/w/cpp/language/override

    0 讨论(0)
  • 2021-01-19 10:57

    In case of pure virtual functions and compilations not really. You will get an error anyway (except in as in the example from Pete)

    But the error message may be more readable if you get an error like "your function does not override anything" compared to later "cannot instantiate abstract class"

    Another advantage would be while reading the declaration you know this is a derived method from a base class.

    Also it is good practice to just get used to declare all overridden methods with override. So why make a difference here and have inconsistent style.

    As to why it is good to have all overriden methods be declared override:

    imagine you have

    class A
    {
        virtual void Foo();
    };
    
    class B: public A
    {
        virtual void Foo() override;
    };
    

    And then you change Foo to a const function in A. Without override this will compile but when you call A->foo() and it is a B object B->foo() will not be called without any indication something changed there. With override you get an error here.

    0 讨论(0)
  • 2021-01-19 11:00

    The main advantage of override here would be to encourage maintainability. Consider below:

    class Foo
    {
    public: 
        virtual void foo() = 0;
    };
    class Derived : public Foo
    {
    public:
        //....
        virtual void foo(double x) override
        {
             //This throws error
        }
    };
    

    As you can see above, the compiler would throw an error if you compiled the above. What would happen is the compiler would complain about the function not having the same signature. Without the override keyword, the outcome would have differed.

    0 讨论(0)
提交回复
热议问题