zoukankan      html  css  js  c++  java
  • final specifier (since C++11)

    Specifies that a virtual function cannot be overridden in a derived class or that a class cannot be inherited from.

    Syntax

    The identifier final, if used, appears immediately after the declarator in the syntax of a member function declaration or a member function definition.

    declarator virt-specifier-seq(optional) pure-specifier(optional) (1)  
     
    declarator virt-specifier-seq(optional) function-body (2)  
     
    class-key attr(optional) class-head-name class-virt-specifier(optional) :base-specifier-list(optional) (3)  
     
    1) In a member function declaration, final may appear in virt-specifier-seq immediately after the declarator, and before the pure-specifier, if used.
    2) In a member function definition, final may appear in virt-specifier-seq immediately after the declarator and just before function-body (which may begin with a member initializer list)
    3) In a class definition, final may appear as class-virt-specifier immediately after the name of the class, just before the colon that begins the base-specifier-list, if used.

    In the cases (1,2), virt-specifier-seq, if used, is either override or final, or final override or override final. In the case (3), the only allowed value of class-virt-specifier, if used, is final

    Explanation

    When used in a virtual function declaration or definition, final ensures that the function is virtual and specifies that it may not be overridden by derived classes. The program is ill-formed (a compile-time error is generated) otherwise.

    When used in a class definition, final specifies that this class may not appear in the base-specifier-list of another class definition (in other words, cannot be derived from). The program is ill-formed (a compile-time error is generated) otherwise. final can also be used with a union definition, in which case it has no effect (other than on the outcome ofstd::is_final), since unions cannot be derived from)

    final is an identifier with a special meaning when used in a member function declaration or class head. In other contexts it is not reserved and may be used to name objects and functions.

    Example

    struct Base
    {
        virtual void foo();
    };
     
    struct A : Base
    {
        virtual void foo() final; // A::foo is final
        void bar() final; // Error: non-virtual function cannot be final
    };
     
    struct B final : A // struct B is final
    {
        void foo(); // Error: foo cannot be overridden as it's final in A
    };
     
    struct C : B // Error: B is final
    {
    };

    See also

  • 相关阅读:
    BZOJ 2002: [Hnoi2010]Bounce 弹飞绵羊
    算法笔记-- 二进制集合枚举子集 && 求子集和 && 求父集和
    BZOJ 1084: [SCOI2005]最大子矩阵
    BZOJ 1968: [Ahoi2005]COMMON 约数研究
    2018 German Collegiate Programming Contest (GCPC 18)
    Codeforces 1100 F
    算法笔记--极大极小搜索及alpha-beta剪枝
    2017 Russian Code Cup (RCC 17), Elimination Round D
    All You Can Code 2008 (Romanian Contest) A
    2016 Russian Code Cup (RCC 16), Final Round B
  • 原文地址:https://www.cnblogs.com/scarecrow-blog/p/5799946.html
Copyright © 2011-2022 走看看