What is the purpose of the “final” keyword in C++11 for functions?

后端 未结 10 2006
故里飘歌
故里飘歌 2020-12-02 05:31

What is the purpose of the final keyword in C++11 for functions? I understand it prevents function overriding by derived classes, but if this is the case, then

10条回答
  •  庸人自扰
    2020-12-02 05:55

    What you are missing, as idljarn already mentioned in a comment is that if you are overriding a function from a base class, then you cannot possibly mark it as non-virtual:

    struct base {
       virtual void f();
    };
    struct derived : base {
       void f() final;       // virtual as it overrides base::f
    };
    struct mostderived : derived {
       //void f();           // error: cannot override!
    };
    

提交回复
热议问题