final specifier

From Cppreference

Jump to: navigation, search

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

Contents

[edit] Syntax

function_declaration final ;
class class_name final base_classes

[edit] Explanation

In a virtual function declaration, it specifies that the function may not be overridden by derived classes

final is an identifier with a special meaning when used in a member function declaration or class head, otherwise it's not reserved.

[edit] Example

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

[edit] See also