Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/language/final"

From cppreference.com
< cpp‎ | language
m (typo)
m (explain class-virt-spec)
Line 14: Line 14:
 
@1@ In a member function declaration, {{tt|final}} may appear in {{spar|virt-specifier-seq}} immediately after the declarator, and before the {{rlp|abstract_class|pure-specifier}}, if used.
 
@1@ In a member function declaration, {{tt|final}} may appear in {{spar|virt-specifier-seq}} immediately after the declarator, and before the {{rlp|abstract_class|pure-specifier}}, if used.
 
@2@ In a member function definition, {{tt|final}} may appear in {{spar|virt-specifier-seq}} immediately after the declarator and just before {{spar|function-body}} (which may begin with a {{rlp|initializer list|member initializer list}})
 
@2@ In a member function definition, {{tt|final}} may appear in {{spar|virt-specifier-seq}} immediately after the declarator and just before {{spar|function-body}} (which may begin with a {{rlp|initializer list|member initializer list}})
@3@ In a class definition, {{tt|final}} may appear immediately after the name of the class, just before the colon that begins the {{spar|base-specifier-list}}, if used.
+
@3@ In a class definition, {{tt|final}} may appear as {{spar|class-virt-specifier}} immediately after the name of the class, just before the colon that begins the {{spar|base-specifier-list}}, if used.
  
In the cases (1,2), {{spar|virt-specifier-seq}}, if used, is either {{rlp|override}} or {{tt|final}}, or {{tt|final override}} or {{tt|override final}}.
+
In the cases (1,2), {{spar|virt-specifier-seq}}, if used, is either {{rlp|override}} or {{tt|final}}, or {{tt|final override}} or {{tt|override final}}. In the case (3), the only allowed value of {{spar|class-virt-specifier}}, if used, is {{tt|final}}
  
 
===Explanation===
 
===Explanation===

Revision as of 08:06, 7 February 2014

 
 
C++ language
General topics
Flow control
Conditional execution statements
if
Iteration statements (loops)
for
range-for (C++11)
Jump statements
Functions
Function declaration
Lambda function expression
inline specifier
Dynamic exception specifications (until C++17*)
noexcept specifier (C++11)
Exceptions
Namespaces
Types
Specifiers
const/volatile
decltype (C++11)
auto (C++11)
constexpr (C++11)
consteval (C++20)
constinit (C++20)
Storage duration specifiers
Initialization
Expressions
Alternative representations
Literals
Boolean - Integer - Floating-point
Character - String - nullptr (C++11)
User-defined (C++11)
Utilities
Attributes (C++11)
Types
typedef declaration
Type alias declaration (C++11)
Casts
Memory allocation
Classes
Class-specific function properties
Virtual function
override specifier (C++11)  
final specifier (C++11)
explicit (C++11)
static

Special member functions
Templates
Miscellaneous
 

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

Contents

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 compiler 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). he program is ill-formed (a compiler error is generated) otherwise.

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 A
{
    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