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

后端 未结 10 1975
故里飘歌
故里飘歌 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:54

    final adds an explicit intent to not have your function overridden, and will cause a compiler error should this be violated:

    struct A {
        virtual int foo(); // #1
    };
    struct B : A {
        int foo();
    };
    

    As the code stands, it compiles, and B::foo overrides A::foo. B::foo is also virtual, by the way. However, if we change #1 to virtual int foo() final, then this is a compiler error, and we are not allowed to override A::foo any further in derived classes.

    Note that this does not allow us to "reopen" a new hierarchy, i.e. there's no way to make B::foo a new, unrelated function that can be independently at the head of a new virtual hierarchy. Once a function is final, it can never be declared again in any derived class.

    0 讨论(0)
  • 2020-12-02 05:54

    Final keyword in C++ when added to a function, prevents it from being overridden by a base class. Also when added to a class prevents inheritance of any type. Consider the following example which shows use of final specifier. This program fails in compilation.

    #include <iostream>
    using namespace std;
    
    class Base
    {
      public:
      virtual void myfun() final
      {
        cout << "myfun() in Base";
      }
    };
    class Derived : public Base
    {
      void myfun()
      {
        cout << "myfun() in Derived\n";
      }
    };
    
    int main()
    {
      Derived d;
      Base &b = d;
      b.myfun();
      return 0;
    }
    

    Also:

    #include <iostream>
    class Base final
    {
    };
    
    class Derived : public Base
    {
    };
    
    int main()
    {
      Derived d;
      return 0;
    }
    
    0 讨论(0)
  • 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!
    };
    
    0 讨论(0)
  • 2020-12-02 06:00
    • It is to prevent a class from being inherited. From Wikipedia:

      C++11 also adds the ability to prevent inheriting from classes or simply preventing overriding methods in derived classes. This is done with the special identifier final. For example:

      struct Base1 final { };
      
      struct Derived1 : Base1 { }; // ill-formed because the class Base1 
                                   // has been marked final
      
    • It is also used to mark a virtual function so as to prevent it from being overridden in the derived classes:

      struct Base2 {
          virtual void f() final;
      };
      
      struct Derived2 : Base2 {
          void f(); // ill-formed because the virtual function Base2::f has 
                    // been marked final
      };
      

    Wikipedia further makes an interesting point:

    Note that neither override nor final are language keywords. They are technically identifiers; they only gain special meaning when used in those specific contexts. In any other location, they can be valid identifiers.

    That means, the following is allowed:

    int const final = 0;     // ok
    int const override = 1;  // ok
    
    0 讨论(0)
提交回复
热议问题